feat(#1591): surface pending-login + crashing-agent banner warnings
This commit is contained in:
parent
5f57ea4ef1
commit
2157c3ae01
4 changed files with 188 additions and 1 deletions
|
|
@ -13,8 +13,12 @@
|
|||
//! pressure, a failed unit, …) is a backend-only change — no frontend
|
||||
//! edit. Keep producers cheap; this runs on every `/api/state` assembly.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::container_view::ContainerView;
|
||||
|
||||
/// One server-level warning for the dashboard's top-of-page banner.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ServerWarning {
|
||||
|
|
@ -65,6 +69,68 @@ pub fn server_warnings() -> Vec<ServerWarning> {
|
|||
out
|
||||
}
|
||||
|
||||
/// Agent-state warnings derived from the live container snapshot the
|
||||
/// dashboard already holds: agents that need a claude login, and agents
|
||||
/// that are crashing. Kept separate from [`server_warnings`] (host
|
||||
/// probes) because the caller owns the container list + crash counts;
|
||||
/// the dashboard concatenates both into one banner list.
|
||||
///
|
||||
/// `crash_counts` is `Coordinator::recent_crash_counts(window)` — agent →
|
||||
/// number of crashes inside that window — so a crash-looping agent shows
|
||||
/// its repeat count rather than a single point-in-time flap.
|
||||
#[must_use]
|
||||
pub fn agent_state_warnings<S: std::hash::BuildHasher>(
|
||||
containers: &[ContainerView],
|
||||
crash_counts: &HashMap<String, usize, S>,
|
||||
) -> Vec<ServerWarning> {
|
||||
let mut out = Vec::new();
|
||||
|
||||
// `needs_login` is already running-gated in `container_view::build_all`,
|
||||
// so a stopped container never lights this.
|
||||
let mut pending: Vec<&str> = containers
|
||||
.iter()
|
||||
.filter(|c| c.needs_login)
|
||||
.map(|c| c.name.as_str())
|
||||
.collect();
|
||||
if !pending.is_empty() {
|
||||
pending.sort_unstable();
|
||||
out.push(ServerWarning {
|
||||
kind: "pending_logins",
|
||||
level: "warn",
|
||||
message: format!(
|
||||
"{n} agent{plural} need claude login: {list} \
|
||||
— run `hivectl login <agent>` to authenticate",
|
||||
n = pending.len(),
|
||||
plural = if pending.len() == 1 { "" } else { "s" },
|
||||
list = pending.join(", "),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
if !crash_counts.is_empty() {
|
||||
let mut crashing: Vec<(&String, usize)> =
|
||||
crash_counts.iter().map(|(a, n)| (a, *n)).collect();
|
||||
crashing.sort_unstable_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(b.0)));
|
||||
let list = crashing
|
||||
.iter()
|
||||
.map(|(a, n)| format!("{a} (×{n})"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
out.push(ServerWarning {
|
||||
kind: "agents_crashing",
|
||||
level: "crit",
|
||||
message: format!(
|
||||
"{n} agent{plural} crashing: {list} \
|
||||
— check the container journal (`hivectl logs <agent>`)",
|
||||
n = crashing.len(),
|
||||
plural = if crashing.len() == 1 { "" } else { "s" },
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
/// Disk usage for the filesystem backing the host nix store — internal to
|
||||
/// the disk-pressure producer above.
|
||||
struct DiskUsage {
|
||||
|
|
@ -122,3 +188,68 @@ fn disk_usage(path: &str) -> Option<DiskUsage> {
|
|||
used_pct,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn cv(name: &str, needs_login: bool) -> ContainerView {
|
||||
ContainerView {
|
||||
name: name.to_owned(),
|
||||
container: format!("h-{name}"),
|
||||
port: 0,
|
||||
running: true,
|
||||
needs_update: false,
|
||||
needs_login,
|
||||
deployed_sha: None,
|
||||
pending_reminders: 0,
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_agent_warnings_when_all_healthy() {
|
||||
let containers = [cv("alice", false), cv("bob", false)];
|
||||
assert!(agent_state_warnings(&containers, &HashMap::new()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_logins_lists_sorted_agents() {
|
||||
let containers = [cv("zoe", true), cv("amy", false), cv("bob", true)];
|
||||
let w = agent_state_warnings(&containers, &HashMap::new());
|
||||
assert_eq!(w.len(), 1);
|
||||
assert_eq!(w[0].kind, "pending_logins");
|
||||
assert_eq!(w[0].level, "warn");
|
||||
// sorted, login-needing only, count reflected
|
||||
assert!(w[0].message.starts_with("2 agents need claude login: bob, zoe"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn singular_grammar_for_one_agent() {
|
||||
let containers = [cv("solo", true)];
|
||||
let w = agent_state_warnings(&containers, &HashMap::new());
|
||||
assert!(w[0].message.starts_with("1 agent needs claude login: solo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crashing_warning_is_crit_and_count_ordered() {
|
||||
let crashes = HashMap::from([("flap".to_owned(), 5), ("blip".to_owned(), 1)]);
|
||||
let w = agent_state_warnings(&[], &crashes);
|
||||
assert_eq!(w.len(), 1);
|
||||
assert_eq!(w[0].kind, "agents_crashing");
|
||||
assert_eq!(w[0].level, "crit");
|
||||
// higher crash count first
|
||||
assert!(w[0].message.contains("flap (×5), blip (×1)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_warnings_coexist() {
|
||||
let containers = [cv("a", true)];
|
||||
let crashes = HashMap::from([("b".to_owned(), 2)]);
|
||||
let kinds: Vec<&str> = agent_state_warnings(&containers, &crashes)
|
||||
.iter()
|
||||
.map(|w| w.kind)
|
||||
.collect();
|
||||
assert_eq!(kinds, ["pending_logins", "agents_crashing"]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue