implement broadcast messaging: send to '*' reaches all agents with hint

This commit is contained in:
damocles 2026-05-16 13:16:13 +02:00
parent a57e500f48
commit abcf7a0c41
4 changed files with 41 additions and 11 deletions

View file

@ -102,15 +102,42 @@ async fn dispatch(req: &AgentRequest, agent: &str, coord: &Arc<Coordinator>) ->
let broker = &coord.broker;
match req {
AgentRequest::Send { to, body } => {
match broker.send(&Message {
from: agent.to_owned(),
to: to.clone(),
body: body.clone(),
}) {
Ok(()) => AgentResponse::Ok,
Err(e) => AgentResponse::Err {
message: format!("{e:#}"),
},
// 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));
}
}
if errors.is_empty() {
AgentResponse::Ok
} else {
AgentResponse::Err {
message: format!("broadcast failed for agents: {}", errors.join(", ")),
}
}
} else {
// Normal unicast send
match broker.send(&Message {
from: agent.to_owned(),
to: to.clone(),
body: body.clone(),
}) {
Ok(()) => AgentResponse::Ok,
Err(e) => AgentResponse::Err {
message: format!("{e:#}"),
},
}
}
}
AgentRequest::Recv { wait_seconds } => match broker

View file

@ -117,6 +117,9 @@ impl Coordinator {
let _ = std::fs::remove_file(&socket.path);
}
}
pub fn list_agents(&self) -> Vec<String> {
self.agents.lock().unwrap().keys().cloned().collect()
}
/// Mark an agent as in-progress (only one state per agent for now).
pub fn set_transient(&self, name: &str, kind: TransientKind) {