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