type Message.from as Ident

This commit is contained in:
damocles 2026-07-22 20:12:50 +02:00 committed by mara
commit 4989bcdb5e
6 changed files with 43 additions and 24 deletions

View file

@ -1025,7 +1025,7 @@ impl Coordinator {
// doesn't bubble out and unwind the topology write. // doesn't bubble out and unwind the topology write.
if let Some(op) = old_parent.as_deref() { if let Some(op) = old_parent.as_deref() {
let _ = self.broker.send(&hive_sh4re::Message { let _ = self.broker.send(&hive_sh4re::Message {
from: hive_sh4re::SYSTEM_SENDER.to_owned(), from: hive_sh4re::trusted_sender(hive_sh4re::SYSTEM_SENDER),
to: op.to_owned(), to: op.to_owned(),
body: format!("{child} moved out of your subtree to {new_label}"), body: format!("{child} moved out of your subtree to {new_label}"),
in_reply_to: None, in_reply_to: None,
@ -1033,7 +1033,7 @@ impl Coordinator {
} }
if let Some(np) = new_parent { if let Some(np) = new_parent {
let _ = self.broker.send(&hive_sh4re::Message { let _ = self.broker.send(&hive_sh4re::Message {
from: hive_sh4re::SYSTEM_SENDER.to_owned(), from: hive_sh4re::trusted_sender(hive_sh4re::SYSTEM_SENDER),
to: np.to_owned(), to: np.to_owned(),
body: format!( body: format!(
"{child} just moved into your subtree (was previously under {old_label})" "{child} just moved into your subtree (was previously under {old_label})"
@ -1087,7 +1087,7 @@ impl Coordinator {
let new_label = new_parent.unwrap_or("<root>"); let new_label = new_parent.unwrap_or("<root>");
if let Some(op) = old_parent.as_deref() { if let Some(op) = old_parent.as_deref() {
let _ = self.broker.send(&hive_sh4re::Message { let _ = self.broker.send(&hive_sh4re::Message {
from: hive_sh4re::SYSTEM_SENDER.to_owned(), from: hive_sh4re::trusted_sender(hive_sh4re::SYSTEM_SENDER),
to: op.to_owned(), to: op.to_owned(),
body: format!("{child} moved out of your subtree to {new_label}"), body: format!("{child} moved out of your subtree to {new_label}"),
in_reply_to: None, in_reply_to: None,
@ -1095,7 +1095,7 @@ impl Coordinator {
} }
if let Some(np) = new_parent { if let Some(np) = new_parent {
let _ = self.broker.send(&hive_sh4re::Message { let _ = self.broker.send(&hive_sh4re::Message {
from: hive_sh4re::SYSTEM_SENDER.to_owned(), from: hive_sh4re::trusted_sender(hive_sh4re::SYSTEM_SENDER),
to: np.to_owned(), to: np.to_owned(),
body: format!( body: format!(
"{child} just moved into your subtree (was previously under {old_label})" "{child} just moved into your subtree (was previously under {old_label})"
@ -1349,7 +1349,7 @@ impl Coordinator {
still in your window." still in your window."
); );
if let Err(e) = self.broker.send(&hive_sh4re::Message { if let Err(e) = self.broker.send(&hive_sh4re::Message {
from: hive_sh4re::SYSTEM_SENDER.to_owned(), from: hive_sh4re::trusted_sender(hive_sh4re::SYSTEM_SENDER),
to: name.to_owned(), to: name.to_owned(),
body, body,
in_reply_to: None, in_reply_to: None,
@ -1402,7 +1402,7 @@ impl Coordinator {
} }
}; };
if let Err(e) = self.broker.send(&hive_sh4re::Message { if let Err(e) = self.broker.send(&hive_sh4re::Message {
from: from.to_owned(), from: hive_sh4re::trusted_sender(from),
to: agent.to_owned(), to: agent.to_owned(),
body, body,
in_reply_to: None, in_reply_to: None,
@ -1424,7 +1424,7 @@ impl Coordinator {
continue; continue;
} }
if let Err(e) = self.broker.send(&hive_sh4re::Message { if let Err(e) = self.broker.send(&hive_sh4re::Message {
from: from.to_owned(), from: hive_sh4re::trusted_sender(from),
to: agent_name.clone(), to: agent_name.clone(),
body: broadcast_body.clone(), body: broadcast_body.clone(),
in_reply_to: None, in_reply_to: None,

View file

@ -164,7 +164,7 @@ pub(super) async fn post_op_send(
)); ));
} }
} else if let Err(e) = state.coord.broker.send(&hive_sh4re::Message { } else if let Err(e) = state.coord.broker.send(&hive_sh4re::Message {
from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(), from: hive_sh4re::trusted_sender(hive_sh4re::OPERATOR_RECIPIENT),
to: to.clone(), to: to.clone(),
body, body,
in_reply_to: None, in_reply_to: None,

View file

@ -321,7 +321,7 @@ async fn handle_recv(
messages: deliveries messages: deliveries
.into_iter() .into_iter()
.map(|d| hive_sh4re::DeliveredMessage { .map(|d| hive_sh4re::DeliveredMessage {
from: d.message.from, from: d.message.from.to_string(),
body: d.message.body, body: d.message.body,
id: d.id, id: d.id,
redelivered: d.redelivered, redelivered: d.redelivered,
@ -347,7 +347,7 @@ fn handle_wake(
body: &str, body: &str,
) -> hive_core_agent_sock::Response { ) -> hive_core_agent_sock::Response {
match coord.broker.send(&Message { match coord.broker.send(&Message {
from: from.to_owned(), from: hive_sh4re::trusted_sender(from),
to: agent.to_owned(), to: agent.to_owned(),
body: body.to_owned(), body: body.to_owned(),
in_reply_to: None, in_reply_to: None,
@ -490,7 +490,7 @@ fn handle_operator_msg(
body: &str, body: &str,
) -> hive_core_agent_sock::Response { ) -> hive_core_agent_sock::Response {
match coord.broker.send(&Message { match coord.broker.send(&Message {
from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(), from: hive_sh4re::trusted_sender(hive_sh4re::OPERATOR_RECIPIENT),
to: agent.to_owned(), to: agent.to_owned(),
body: body.to_owned(), body: body.to_owned(),
in_reply_to: None, in_reply_to: None,
@ -955,7 +955,7 @@ pub(crate) fn fan_out_send(
continue; continue;
} }
if let Err(e) = coord.broker.send(&Message { if let Err(e) = coord.broker.send(&Message {
from: from.to_owned(), from: hive_sh4re::trusted_sender(from),
to: target.clone(), to: target.clone(),
body: body.to_owned(), body: body.to_owned(),
in_reply_to, in_reply_to,
@ -1040,7 +1040,7 @@ pub(crate) fn handle_send(
} }
} }
match coord.broker.send(&Message { match coord.broker.send(&Message {
from: agent.to_owned(), from: hive_sh4re::trusted_sender(agent),
to: resolved, to: resolved,
body: body.to_owned(), body: body.to_owned(),
in_reply_to, in_reply_to,

View file

@ -277,12 +277,12 @@ impl Broker {
// Operator messages get elevated priority so they surface before // Operator messages get elevated priority so they surface before
// queued wakes (bash completions, forge events, etc.) when the // queued wakes (bash completions, forge events, etc.) when the
// harness pops the next turn driver. All other senders stay at 0. // harness pops the next turn driver. All other senders stay at 0.
let priority: i64 = i64::from(message.from == "operator"); let priority: i64 = i64::from(message.from.as_str() == hive_sh4re::OPERATOR_RECIPIENT);
conn.execute( conn.execute(
"INSERT INTO messages (sender, recipient, body, sent_at, in_reply_to, priority) \ "INSERT INTO messages (sender, recipient, body, sent_at, in_reply_to, priority) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6)", VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![ params![
message.from, message.from.as_str(),
message.to, message.to,
message.body, message.body,
now, now,
@ -294,7 +294,7 @@ impl Broker {
drop(conn); drop(conn);
let _ = self.events.send(MessageEvent::Sent { let _ = self.events.send(MessageEvent::Sent {
id: row_id, id: row_id,
from: message.from.clone(), from: message.from.to_string(),
to: message.to.clone(), to: message.to.clone(),
body: message.body.clone(), body: message.body.clone(),
at: now, at: now,
@ -664,7 +664,7 @@ impl Broker {
id, id,
redelivered, redelivered,
message: Message { message: Message {
from, from: hive_sh4re::trusted_sender(&from),
to, to,
body, body,
in_reply_to, in_reply_to,
@ -678,7 +678,7 @@ impl Broker {
for d in &deliveries { for d in &deliveries {
let _ = self.events.send(MessageEvent::Delivered { let _ = self.events.send(MessageEvent::Delivered {
id: d.id, id: d.id,
from: d.message.from.clone(), from: d.message.from.to_string(),
to: d.message.to.clone(), to: d.message.to.clone(),
body: d.message.body.clone(), body: d.message.body.clone(),
at: now, at: now,
@ -1187,7 +1187,7 @@ mod tests {
fn msg(from: &str, to: &str, body: &str) -> Message { fn msg(from: &str, to: &str, body: &str) -> Message {
Message { Message {
from: from.to_owned(), from: hive_types::Ident::parse(from).expect("test sender must be a valid ident"),
to: to.to_owned(), to: to.to_owned(),
body: body.to_owned(), body: body.to_owned(),
in_reply_to: None, in_reply_to: None,
@ -1475,7 +1475,7 @@ mod tests {
assert_eq!(batch.len(), 3); assert_eq!(batch.len(), 3);
// Operator message surfaces first despite arriving last. // Operator message surfaces first despite arriving last.
assert_eq!(batch[0].message.body, "stop what you're doing"); assert_eq!(batch[0].message.body, "stop what you're doing");
assert_eq!(batch[0].message.from, "operator"); assert_eq!(batch[0].message.from.as_str(), "operator");
// Remaining two in FIFO order. // Remaining two in FIFO order.
assert_eq!(batch[1].message.body, "task done"); assert_eq!(batch[1].message.body, "task done");
assert_eq!(batch[2].message.body, "new pr"); assert_eq!(batch[2].message.body, "new pr");

View file

@ -129,7 +129,7 @@ fn fire_schedule(coord: &Arc<Coordinator>, schedule: &Schedule, now: i64) {
Ok(false) => {} Ok(false) => {}
} }
let msg = Message { let msg = Message {
from: "scheduled".to_owned(), from: hive_sh4re::trusted_sender("scheduled"),
to: target.clone(), to: target.clone(),
body: schedule.body.clone(), body: schedule.body.clone(),
in_reply_to: None, in_reply_to: None,
@ -227,7 +227,7 @@ fn notify_operator_missing_target(coord: &Coordinator, schedule: &Schedule, targ
body = schedule.body body = schedule.body
); );
let msg = Message { let msg = Message {
from: "scheduled".to_owned(), from: hive_sh4re::trusted_sender("scheduled"),
to: hive_sh4re::OPERATOR_RECIPIENT.to_owned(), to: hive_sh4re::OPERATOR_RECIPIENT.to_owned(),
body, body,
in_reply_to: None, in_reply_to: None,
@ -327,7 +327,7 @@ pub async fn fire_now(
continue; continue;
} }
let msg = Message { let msg = Message {
from: "scheduled".to_owned(), from: hive_sh4re::trusted_sender("scheduled"),
to: target.clone(), to: target.clone(),
body: schedule.body.clone(), body: schedule.body.clone(),
in_reply_to: None, in_reply_to: None,

View file

@ -155,7 +155,7 @@ pub struct ReminderStats {
/// A logical message between agents. /// A logical message between agents.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message { pub struct Message {
pub from: String, pub from: Ident,
pub to: String, pub to: String,
pub body: String, pub body: String,
/// Optional broker row-id of the message this is a reply to. /// Optional broker row-id of the message this is a reply to.
@ -442,6 +442,25 @@ pub const CHILDREN_RECIPIENT: &str = "<children>";
/// Manager harness recognises this and parses the body as a `HelperEvent`. /// Manager harness recognises this and parses the body as a `HelperEvent`.
pub const SYSTEM_SENDER: &str = "system"; pub const SYSTEM_SENDER: &str = "system";
/// Parse `s` as a [`Ident`] for use as `Message.from`, falling back to
/// [`SYSTEM_SENDER`] on the (should-be-unreachable) case that `s` isn't
/// ident-shaped. `Message.from` is always either a fixed sentinel literal
/// (`SYSTEM_SENDER`, `OPERATOR_RECIPIENT`, `"scheduled"`, …) or an
/// already-registered agent's own name reaching this point through
/// hive-c0re's internal dispatch — never arbitrary external input — so
/// this is a defensive fallback for a programming-bug case, not a
/// validation gate.
///
/// # Panics
///
/// Never, unless [`SYSTEM_SENDER`] itself stops being ident-shaped (which
/// would also be a programming bug, caught by `hive-types`' own tests).
#[must_use]
pub fn trusted_sender(s: &str) -> Ident {
Ident::parse(s)
.unwrap_or_else(|_| Ident::parse(SYSTEM_SENDER).expect("SYSTEM_SENDER is a valid Ident"))
}
/// Out-of-band events the host-side daemon pushes to the manager's inbox. /// Out-of-band events the host-side daemon pushes to the manager's inbox.
/// Serialised as JSON in `Message::body` (sender = `SYSTEM_SENDER`). /// Serialised as JSON in `Message::body` (sender = `SYSTEM_SENDER`).
/// Per-variant triggers + the optional `sha`/`tag` semantics live in /// Per-variant triggers + the optional `sha`/`tag` semantics live in