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 } => {
// Handle broadcast sends (recipient = "*")
if to == "*" {
let agents = coord.list_agents();
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));
}
}
let errors = coord.broadcast_send(agent, body);
if errors.is_empty() {
AgentResponse::Ok
} 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 {
PathBuf::from(format!("{AGENT_RUNTIME_ROOT}/{name}"))
}

View file

@ -85,7 +85,18 @@ fn manager_recv_timeout(wait_seconds: Option<u64>) -> std::time::Duration {
#[allow(clippy::too_many_lines)]
async fn dispatch(req: &ManagerRequest, coord: &Arc<Coordinator>) -> ManagerResponse {
match req {
ManagerRequest::Send { to, body } => match coord.broker.send(&Message {
ManagerRequest::Send { to, body } => {
if to == "*" {
let errors = coord.broadcast_send(MANAGER_AGENT, body);
if errors.is_empty() {
ManagerResponse::Ok
} else {
ManagerResponse::Err {
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(),
@ -94,7 +105,9 @@ async fn dispatch(req: &ManagerRequest, coord: &Arc<Coordinator>) -> ManagerResp
Err(e) => ManagerResponse::Err {
message: format!("{e:#}"),
},
},
}
}
}
ManagerRequest::OperatorMsg { body } => match coord.broker.send(&Message {
from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(),
to: MANAGER_AGENT.to_owned(),