diff --git a/src/agent/channel.rs b/src/agent/channel.rs index 963fdeab8..0b80a1c7f 100644 --- a/src/agent/channel.rs +++ b/src/agent/channel.rs @@ -2959,8 +2959,11 @@ impl Channel { }) } - /// Send outbound text and record send metrics. - async fn send_outbound_text(&self, text: String, error_context: &str) { + /// Send outbound text and record send metrics. Returns `true` on success. + /// + /// Failure here means the outbound response channel's receiver has closed + /// (e.g. shutdown) — `mpsc::Sender::send` does not fail transiently. + async fn send_outbound_text(&self, text: String, error_context: &str) -> bool { match self.send_routed(OutboundResponse::Text(text)).await { Ok(()) => { #[cfg(feature = "metrics")] @@ -2971,6 +2974,7 @@ impl Channel { .with_label_values(&[&self.deps.agent_id, channel_type]) .inc(); } + true } Err(error) => { #[cfg(feature = "metrics")] @@ -2982,6 +2986,7 @@ impl Channel { .inc(); } tracing::error!(%error, channel_id = %self.id, "{error_context}"); + false } } } @@ -3061,11 +3066,15 @@ impl Channel { self.state .conversation_logger .log_bot_message(&self.state.channel_id, &final_text); - self.send_outbound_text( - final_text, - "failed to send retrigger fallback reply", - ) - .await; + if self + .send_outbound_text( + final_text, + "failed to send retrigger fallback reply", + ) + .await + { + replied_flag.store(true, std::sync::atomic::Ordering::Relaxed); + } } } } else { @@ -3126,11 +3135,15 @@ impl Channel { self.state .conversation_logger .log_bot_message(&self.state.channel_id, &final_text); - self.send_outbound_text( - final_text, - "failed to send retrigger fallback reply", - ) - .await; + if self + .send_outbound_text( + final_text, + "failed to send retrigger fallback reply", + ) + .await + { + replied_flag.store(true, std::sync::atomic::Ordering::Relaxed); + } } } } else { @@ -3186,8 +3199,11 @@ impl Channel { Some(self.agent_display_name()), tool_calls_json, ); - self.send_outbound_text(final_text, "failed to send fallback reply") - .await; + self.send_outbound_text( + final_text, + "failed to send fallback reply", + ) + .await; } }