huskies: merge 1149 story huskies health chat command — surface gateway, sled, matrix, creds, and build-hash status
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::services::Services;
|
||||
use matrix_sdk::ruma::{OwnedEventId, OwnedRoomId, OwnedUserId};
|
||||
use std::collections::{BTreeMap, HashSet, VecDeque};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicI64;
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
@@ -104,6 +105,10 @@ pub struct BotContext {
|
||||
/// Used by the "rebuild gateway" command to construct the health-check URL
|
||||
/// passed to the trampoline. `None` in standalone single-project mode.
|
||||
pub gateway_port: Option<u16>,
|
||||
/// Timestamp (ms since Unix epoch) of the last Matrix event received in any
|
||||
/// configured room. Updated atomically on every `on_room_message` call so
|
||||
/// the `health` command can detect a stale or dead sync loop.
|
||||
pub last_matrix_event_ms: Arc<AtomicI64>,
|
||||
}
|
||||
|
||||
impl BotContext {
|
||||
@@ -299,6 +304,7 @@ mod tests {
|
||||
SEEN_EVENT_IDS_CAP,
|
||||
))),
|
||||
gateway_port: None,
|
||||
last_matrix_event_ms: Arc::new(AtomicI64::new(chrono::Utc::now().timestamp_millis())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,20 @@ use super::super::verification::check_sender_verified;
|
||||
|
||||
use super::handle_message;
|
||||
|
||||
/// Return `true` when the message is a `health` command addressed to the bot.
|
||||
///
|
||||
/// Recognised case-insensitively as the single word `health` after stripping the bot
|
||||
/// mention prefix. Any trailing whitespace is ignored; extra arguments are not
|
||||
/// expected and are silently discarded.
|
||||
fn extract_health_command(message: &str, bot_name: &str, bot_user_id: &str) -> bool {
|
||||
let stripped = crate::chat::util::strip_bot_mention(message, bot_name, bot_user_id);
|
||||
let trimmed = stripped
|
||||
.trim()
|
||||
.trim_start_matches(|c: char| !c.is_alphanumeric());
|
||||
let cmd = trimmed.split_whitespace().next().unwrap_or("");
|
||||
cmd.eq_ignore_ascii_case("health")
|
||||
}
|
||||
|
||||
/// Return `true` when the message is a "rebuild gateway" command addressed to the bot.
|
||||
///
|
||||
/// The command is recognised case-insensitively as `rebuild gateway` after stripping
|
||||
@@ -100,6 +114,12 @@ pub(in crate::chat::transport::matrix::bot) async fn on_room_message(
|
||||
return;
|
||||
}
|
||||
|
||||
// Update last-event timestamp so the `health` command can detect a stale sync loop.
|
||||
ctx.last_matrix_event_ms.store(
|
||||
chrono::Utc::now().timestamp_millis(),
|
||||
std::sync::atomic::Ordering::Relaxed,
|
||||
);
|
||||
|
||||
// Ignore the bot's own messages to prevent echo loops.
|
||||
if ev.sender == ctx.matrix_user_id {
|
||||
return;
|
||||
@@ -249,6 +269,7 @@ pub(in crate::chat::transport::matrix::bot) async fn on_room_message(
|
||||
"config",
|
||||
"project-rebuild",
|
||||
"upgrade",
|
||||
"health",
|
||||
];
|
||||
|
||||
let stripped = crate::chat::util::strip_bot_mention(
|
||||
@@ -546,6 +567,26 @@ pub(in crate::chat::transport::matrix::bot) async fn on_room_message(
|
||||
return;
|
||||
}
|
||||
|
||||
// `health` — async subsystem health report (gateway + standalone).
|
||||
if extract_health_command(
|
||||
&user_message,
|
||||
&ctx.services.bot_name,
|
||||
ctx.matrix_user_id.as_str(),
|
||||
) {
|
||||
slog!("[matrix-bot] Handling 'health' from {sender}");
|
||||
let response = super::super::super::health::run_health_check(&ctx).await;
|
||||
let html = markdown_to_html(&response);
|
||||
if let Ok(msg_id) = ctx
|
||||
.transport
|
||||
.send_message(&room_id_str, &response, &html)
|
||||
.await
|
||||
&& let Ok(event_id) = msg_id.parse()
|
||||
{
|
||||
ctx.bot_sent_event_ids.lock().await.insert(event_id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for bot-level commands (help, status, ambient, …) before invoking
|
||||
// the LLM. All commands are registered in commands.rs — no special-casing
|
||||
// needed here.
|
||||
|
||||
@@ -6,7 +6,7 @@ use matrix_sdk::ruma::OwnedRoomId;
|
||||
use matrix_sdk::{Client, LoopCtrl, config::SyncSettings};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
use tokio::sync::{RwLock, watch};
|
||||
|
||||
@@ -336,6 +336,7 @@ pub async fn run_bot(
|
||||
super::context::SEEN_EVENT_IDS_CAP,
|
||||
))),
|
||||
gateway_port,
|
||||
last_matrix_event_ms: Arc::new(AtomicI64::new(chrono::Utc::now().timestamp_millis())),
|
||||
};
|
||||
|
||||
slog!(
|
||||
|
||||
Reference in New Issue
Block a user