Fix stale alerts for deleted agents on command deck

This commit is contained in:
AetherForge
2026-06-02 22:09:09 -07:00
parent 96f683a72a
commit 9407efd960
3 changed files with 35 additions and 0 deletions

View File

@@ -194,6 +194,30 @@ func (e *Evaluator) ActiveAlerts() []AlertEvent {
return out
}
// ClearAgent removes all in-memory alert state for a specific agent.
// Call this when an agent is deleted so stale alerts stop appearing on the
// dashboard for machines that no longer exist.
func (e *Evaluator) ClearAgent(agentID string) {
e.mu.Lock()
defer e.mu.Unlock()
// Remove cached alerts for this agent.
filtered := e.activeAlerts[:0]
for _, a := range e.activeAlerts {
if a.AgentID != agentID {
filtered = append(filtered, a)
}
}
e.activeAlerts = filtered
// Remove cooldown + baseline entries so the agent's next appearance
// (e.g. re-registration) starts fresh.
delete(e.baseline, agentID)
for k := range e.lastFired {
if len(k) > len(agentID) && k[len(k)-len(agentID):] == agentID {
delete(e.lastFired, k)
}
}
}
func formatPct(v float64) string {
if v < 0 {
v = 0

View File

@@ -522,6 +522,11 @@ func (f *FleetHandler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
http.Error(w, "delete failed: "+err.Error(), http.StatusInternalServerError)
return
}
// Clear stale in-memory alerts so the machine stops showing up in the
// dashboard alert banner after deletion.
if f.alerts != nil {
f.alerts.ClearAgent(id)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]bool{"success": true})
}
@@ -543,6 +548,9 @@ func (f *FleetHandler) BulkDeleteAgents(w http.ResponseWriter, r *http.Request)
}
if err := f.db.DeleteAgent(id); err == nil {
deleted++
if f.alerts != nil {
f.alerts.ClearAgent(id)
}
}
}
w.Header().Set("Content-Type", "application/json")