fix broadcast send for manager, deduplicate into coordinator.broadcast_send
This commit is contained in:
parent
f3739d2b8e
commit
1a36c38a54
3 changed files with 45 additions and 25 deletions
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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}"))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,18 @@ 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 } => {
|
||||||
|
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(),
|
from: MANAGER_AGENT.to_owned(),
|
||||||
to: to.clone(),
|
to: to.clone(),
|
||||||
body: body.clone(),
|
body: body.clone(),
|
||||||
|
|
@ -94,7 +105,9 @@ async fn dispatch(req: &ManagerRequest, coord: &Arc<Coordinator>) -> ManagerResp
|
||||||
Err(e) => ManagerResponse::Err {
|
Err(e) => ManagerResponse::Err {
|
||||||
message: format!("{e:#}"),
|
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(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue