feat(#1137): rich unread summary in loose ends and wake signal

- hive-sh4re: UnreadMatrix gains summary: String field (per-room breakdown)
- hive-matrix-mcp/protocol: add RoomUnread struct + UnreadSummary request
- hive-matrix-mcp/handlers: collect_unread() fetches per-room data;
  single-unread rooms include truncated last-message body + sender;
  multi-unread rooms carry count only
- hive-matrix-mcp/wake: format_unread_summary() builds wake body from
  RoomUnread slice; terse one-liner for single-room/single-message,
  bulleted list for multi-room; always appends read-hint
- hive-matrix-mcp/timeline: wake body now covers all rooms with unread
  at fire time, not just the triggering event; falls back to per-event
  teaser if notification counts haven't updated yet
- hive-ag3nt/mcp: matrix_unread_summary() replaces matrix_unread_rooms();
  UnreadMatrix loose end carries per-room summary lines; render shows
  room breakdown with sender: body for single-unread rooms
This commit is contained in:
atlas 2026-06-03 12:52:24 +02:00
commit 68e30b857c
19 changed files with 421 additions and 108 deletions

View file

@ -16,18 +16,20 @@ use matrix_sdk::{
ruma::events::room::message::{MessageType, OriginalSyncRoomMessageEvent},
};
use crate::wake;
use crate::{handlers, wake};
/// Install the room-message handler on `client`. Fires on every
/// `m.room.message` event in a joined room; non-self text messages
/// trigger a wake signal to the hyperhive harness via the unix socket
/// at `hyperhive_socket`.
/// `m.room.message` event in a joined room; non-self messages trigger
/// a wake signal to the hyperhive harness via the unix socket at
/// `hyperhive_socket`. The wake body summarises ALL rooms with unread
/// notifications at wake time (not just the triggering event) so the
/// agent receives a full picture in one prompt.
pub fn install_message_handler(client: &Client, hyperhive_socket: PathBuf) {
let socket = Arc::new(hyperhive_socket);
let own_user = client.user_id().map(std::borrow::ToOwned::to_owned);
client.add_event_handler({
let socket = socket.clone();
move |event: OriginalSyncRoomMessageEvent, room: Room, _client: Client| {
move |event: OriginalSyncRoomMessageEvent, room: Room, client: Client| {
let socket = socket.clone();
let own_user = own_user.clone();
async move {
@ -39,21 +41,29 @@ pub fn install_message_handler(client: &Client, hyperhive_socket: PathBuf) {
if own_user.as_ref().is_some_and(|u| u == &event.sender) {
return;
}
let text = match &event.content.msgtype {
MessageType::Text(t) => t.body.clone(),
MessageType::Notice(n) => n.body.clone(),
MessageType::Emote(e) => format!("* {}", e.body),
_ => {
// Non-text content (image / file / location / etc.) —
// still wake, but with a placeholder body so the
// agent knows something landed and can read_room.
format!("[{}]", event.content.msgtype())
}
// Build a wake body that covers all rooms with unread
// notifications, not just the triggering event. This lets
// the agent see the full backlog in a single wake prompt.
// Falls back to the per-event teaser if the collect fails
// (empty result means daemon is not seeing any unread yet —
// unlikely but possible during a sync race).
let unread = handlers::collect_unread(&client).await;
let body = if unread.is_empty() {
// Sync hasn't updated notification counts yet; fall back
// to the current event so the agent still wakes.
let text = match &event.content.msgtype {
MessageType::Text(t) => t.body.clone(),
MessageType::Notice(n) => n.body.clone(),
MessageType::Emote(e) => format!("* {}", e.body),
_ => format!("[{}]", event.content.msgtype()),
};
let room_label = room
.canonical_alias()
.map_or_else(|| room.room_id().to_string(), |a| a.to_string());
wake::format_wake_body(event.sender.as_str(), &room_label, &text)
} else {
wake::format_unread_summary(&unread)
};
let room_label = room
.canonical_alias()
.map_or_else(|| room.room_id().to_string(), |a| a.to_string());
let body = wake::format_wake_body(event.sender.as_str(), &room_label, &text);
if let Err(e) = wake::send_wake(&socket, &body).await {
tracing::warn!(error = %e, "failed to deliver matrix wake to hyperhive");
} else {