fix: wake agent on matrix invite instead of auto-accepting

This commit is contained in:
atlas 2026-06-03 22:16:22 +02:00 committed by mara
commit 06b4745d76

View file

@ -76,22 +76,22 @@ pub fn install_message_handler(client: &Client, hyperhive_socket: PathBuf) {
tracing::info!("matrix message handler installed"); tracing::info!("matrix message handler installed");
} }
/// Install a handler that auto-accepts room invites as soon as they /// Install a handler that wakes the agent when a room invite arrives.
/// arrive during sync. When the daemon receives an `m.room.member` /// The agent decides whether to accept by calling `join_room`.
/// state event with `membership: invite` for the agent's own user ID,
/// it calls `join_room_by_id` immediately and fires a hyperhive wake
/// so the agent learns about the new room on its next turn.
/// ///
/// This closes the gap where agents were provisioned and invited to the /// When the daemon receives an `m.room.member` state event with
/// hive Space by hive-c0re but never received the invite notification /// `membership: invite` for the agent's own user ID during sync, it
/// because the handler wasn't registered — the invite sat in the /// fires a hyperhive wake so the agent can inspect the invite via
/// `invited_rooms()` list forever until the agent called `join_room` /// `list_invites` and act on it with `join_room`.
/// manually. ///
/// This closes the gap where invites accumulated in `invited_rooms()`
/// forever without the agent being notified — the daemon's message
/// handler only fires for joined rooms.
pub fn install_invite_handler(client: &Client, hyperhive_socket: PathBuf) { pub fn install_invite_handler(client: &Client, hyperhive_socket: PathBuf) {
let socket = Arc::new(hyperhive_socket); let socket = Arc::new(hyperhive_socket);
let own_user = client.user_id().map(std::borrow::ToOwned::to_owned); let own_user = client.user_id().map(std::borrow::ToOwned::to_owned);
client.add_event_handler({ client.add_event_handler({
move |event: StrippedRoomMemberEvent, room: Room, client: Client| { move |event: StrippedRoomMemberEvent, room: Room, _client: Client| {
let socket = socket.clone(); let socket = socket.clone();
let own_user = own_user.clone(); let own_user = own_user.clone();
async move { async move {
@ -106,24 +106,17 @@ pub fn install_invite_handler(client: &Client, hyperhive_socket: PathBuf) {
return; return;
} }
let room_id = room.room_id().to_owned(); let room_id = room.room_id().to_owned();
match client.join_room_by_id(&room_id).await { let label = room.name().unwrap_or_else(|| room_id.to_string());
Ok(_) => { tracing::info!(%room_id, "matrix: received room invite, waking agent");
tracing::info!(%room_id, "matrix: auto-accepted invite"); let body = format!(
let label = room.name().unwrap_or_else(|| room_id.to_string()); "[matrix] invited to {label} ({room_id}) — \
let body = format!( use list_invites to see pending invites, join_room to accept"
"[matrix] invited to {label} — auto-accepted; \ );
use read_room to view messages" if let Err(e) = wake::send_wake(&socket, &body).await {
); tracing::warn!(
if let Err(e) = wake::send_wake(&socket, &body).await { error = %e,
tracing::warn!( "matrix: failed to deliver invite-wake to hyperhive"
error = %e, );
"matrix: failed to deliver invite-wake to hyperhive"
);
}
}
Err(e) => {
tracing::warn!(error = %e, %room_id, "matrix: auto-accept invite failed");
}
} }
} }
} }