fix broadcast send for manager, deduplicate into coordinator.broadcast_send

This commit is contained in:
damocles 2026-05-16 19:31:53 +02:00
parent f3739d2b8e
commit 1a36c38a54
3 changed files with 45 additions and 25 deletions

View file

@ -104,21 +104,7 @@ async fn dispatch(req: &AgentRequest, agent: &str, coord: &Arc<Coordinator>) ->
AgentRequest::Send { to, body } => { AgentRequest::Send { to, body } => {
// Handle broadcast sends (recipient = "*") // Handle broadcast sends (recipient = "*")
if to == "*" { if to == "*" {
let agents = coord.list_agents(); let errors = coord.broadcast_send(agent, body);
let broadcast_hint = "\n\n⚠️ _hint: this was a broadcast and may not need any action from you_";
let broadcast_body = format!("{}{}", body, broadcast_hint);
let mut errors = Vec::new();
for agent_name in agents {
if let Err(e) = broker.send(&Message {
from: agent.to_owned(),
to: agent_name.clone(),
body: broadcast_body.clone(),
}) {
errors.push(format!("{}: {e}", agent_name));
}
}
if errors.is_empty() { if errors.is_empty() {
AgentResponse::Ok AgentResponse::Ok
} else { } else {

View file

@ -192,6 +192,27 @@ impl Coordinator {
} }
} }
/// Deliver `body` to every currently-registered agent, appending the
/// standard broadcast hint. Returns a list of per-agent error strings
/// for any that failed (empty = all ok). The sender's own inbox is
/// included — the hint text tells agents to ignore if no action needed.
pub fn broadcast_send(&self, from: &str, body: &str) -> Vec<String> {
const HINT: &str =
"\n\n⚠️ _hint: this was a broadcast and may not need any action from you_";
let broadcast_body = format!("{body}{HINT}");
let mut errors = Vec::new();
for agent_name in self.list_agents() {
if let Err(e) = self.broker.send(&hive_sh4re::Message {
from: from.to_owned(),
to: agent_name.clone(),
body: broadcast_body.clone(),
}) {
errors.push(format!("{agent_name}: {e}"));
}
}
errors
}
pub fn agent_dir(name: &str) -> PathBuf { pub fn agent_dir(name: &str) -> PathBuf {
PathBuf::from(format!("{AGENT_RUNTIME_ROOT}/{name}")) PathBuf::from(format!("{AGENT_RUNTIME_ROOT}/{name}"))
} }

View file

@ -85,16 +85,29 @@ fn manager_recv_timeout(wait_seconds: Option<u64>) -> std::time::Duration {
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
async fn dispatch(req: &ManagerRequest, coord: &Arc<Coordinator>) -> ManagerResponse { async fn dispatch(req: &ManagerRequest, coord: &Arc<Coordinator>) -> ManagerResponse {
match req { match req {
ManagerRequest::Send { to, body } => match coord.broker.send(&Message { ManagerRequest::Send { to, body } => {
from: MANAGER_AGENT.to_owned(), if to == "*" {
to: to.clone(), let errors = coord.broadcast_send(MANAGER_AGENT, body);
body: body.clone(), if errors.is_empty() {
}) { ManagerResponse::Ok
Ok(()) => ManagerResponse::Ok, } else {
Err(e) => ManagerResponse::Err { ManagerResponse::Err {
message: format!("{e:#}"), message: format!("broadcast failed for agents: {}", errors.join(", ")),
}, }
}, }
} else {
match coord.broker.send(&Message {
from: MANAGER_AGENT.to_owned(),
to: to.clone(),
body: body.clone(),
}) {
Ok(()) => ManagerResponse::Ok,
Err(e) => ManagerResponse::Err {
message: format!("{e:#}"),
},
}
}
}
ManagerRequest::OperatorMsg { body } => match coord.broker.send(&Message { ManagerRequest::OperatorMsg { body } => match coord.broker.send(&Message {
from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(), from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(),
to: MANAGER_AGENT.to_owned(), to: MANAGER_AGENT.to_owned(),