carlos / platform Public

console: a rule switched Off sends nothing already queued (R-42)

Saving a rule Off deletes its incidents' unsent fire, clear, update and
reminder rows in the save's transaction, and the drain checks the rule
again before sending an incident's row, dropping it when the rule is off.
A fire waiting on a retry when the rule goes Off is never sent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 69a0b6e6a302b3c39da0a517228d8e77bb8ff25a parent 6669ad0
3 files changed, +116 −1
  • internal/console/alertnotify.go +15 −0
  • internal/console/alertnotify_test.go +76 −0
  • internal/console/alertstore.go +25 −1
diff --git a/internal/console/alertnotify.go b/internal/console/alertnotify.go
index 2811666c8..41b8f6d98 100644
--- a/internal/console/alertnotify.go
+++ b/internal/console/alertnotify.go
@@ -488,6 +488,10 @@ func (s *Server) alertDrainOutbox(ctx context.Context) error {
}
}
+// alertIncidentKinds are the outbox kinds an incident's rule governs: a rule
+// that is off sends none of them (ruling R-42).
+var alertIncidentKinds = map[string]bool{alertKindFire: true, alertKindClear: true, alertKindUpdate: true, alertKindReminder: true}
+
// alertRowOutcome is what drainAlertRow did with a row.
type alertRowOutcome int
@@ -509,6 +513,17 @@ func (s *Server) drainAlertRow(ctx context.Context, r alertOutboxRow) (alertRowO
if revoked, err := s.alertOwnerRevoked(c); err != nil || revoked {
return alertRowSkipped, err // deleted with its rows
}
+ if alertIncidentKinds[r.Kind] && r.IncidentID != 0 {
+ // The rule is read again here, not trusted from when the row was
+ // queued (ruling R-42): one switched off since sends nothing.
+ off, err := s.store.AlertIncidentRuleOff(r.IncidentID)
+ if err != nil {
+ return alertRowSkipped, err
+ }
+ if off {
+ return alertRowSkipped, s.store.DropOutbox(r.ID)
+ }
+ }
if hold := alertWithheld(c, r.Kind); hold != "" {
return alertRowSkipped, s.holdAlertRows(c, hold, now)
}
diff --git a/internal/console/alertnotify_test.go b/internal/console/alertnotify_test.go
index 4623163a0..271aee096 100644
--- a/internal/console/alertnotify_test.go
+++ b/internal/console/alertnotify_test.go
@@ -535,6 +535,82 @@ func TestDrainSealHoldMarksReentry(t *testing.T) {
}
}
+// alertDiskETAIncident stores an open disk_eta incident and returns its id.
+func (nf *notifyFixture) alertDiskETAIncident(t *testing.T) int64 {
+ t.Helper()
+ in := alertIncident{Scope: alertScopeFleet, Rule: "disk_eta", Level: "warning", OpenedAt: nf.now, NotifyAt: nf.now,
+ Members: []string{"box-1"}, Notified: true}
+ if err := nf.st.CommitAlertStep([]alertIncident{in}, nil, nil); err != nil {
+ t.Fatal(err)
+ }
+ var id int64
+ if err := nf.st.db.QueryRow(`SELECT MAX(id) FROM alert_incidents`).Scan(&id); err != nil {
+ t.Fatal(err)
+ }
+ return id
+}
+
+// Ruling R-42: switching a rule Off cancels its incidents' queued rows in
+// the save's transaction. A fire waiting on a retry is never sent; a row
+// that is no incident's is kept.
+func TestRuleOffCancelsQueuedIncidentRows(t *testing.T) {
+ nf := newNotifyFixture(t)
+ id := nf.addHook(t, alertConnectorWebhook, "https://alerts.example.com/in", true)
+ inc := nf.alertDiskETAIncident(t)
+ nf.queue(t, id, inc, alertKindFire, fireNote())
+ nf.hook.setStatus(http.StatusInternalServerError)
+ nf.drain(t)
+ if got := nf.outbox(t, id); len(got) != 1 || got[0].sent || got[0].attempts != 1 {
+ t.Fatalf("outbox = %+v, want the fire waiting on its retry", got)
+ }
+ for _, kind := range []string{alertKindUpdate, alertKindReminder, alertKindClear} {
+ nf.queue(t, id, inc, kind, fireNote())
+ }
+ nf.queue(t, id, 0, "suspended", fireNote())
+ if err := nf.st.SetAlertRule(alertScopeFleet, "disk_eta", alertRuleSetting{Enabled: false}); err != nil {
+ t.Fatal(err)
+ }
+ if got := nf.outbox(t, id); len(got) != 1 || got[0].kind != "suspended" {
+ t.Fatalf("outbox after the rule went off = %+v, want only the suspended notice", got)
+ }
+ nf.hook.setStatus(http.StatusOK)
+ nf.now = nf.now.Add(time.Hour)
+ nf.drain(t)
+ reqs := nf.hook.requests()
+ if len(reqs) != 2 || !strings.Contains(string(reqs[1].body), `"suspended"`) {
+ t.Fatalf("%d sends, want the failed fire then the notice alone", len(reqs))
+ }
+}
+
+// Ruling R-42: the drain checks the rule again before sending an
+// incident's row, and drops one whose rule is off: a step that read the
+// rule on just before the save cannot slip a message through.
+func TestDrainDropsRowOfRuleTurnedOff(t *testing.T) {
+ nf := newNotifyFixture(t)
+ id := nf.addHook(t, alertConnectorWebhook, "https://alerts.example.com/in", true)
+ inc := nf.alertDiskETAIncident(t)
+ if err := nf.st.SetAlertRule(alertScopeFleet, "disk_eta", alertRuleSetting{Enabled: false}); err != nil {
+ t.Fatal(err)
+ }
+ nf.queue(t, id, inc, alertKindFire, fireNote())
+ nf.drain(t)
+ if n := len(nf.hook.requests()); n != 0 {
+ t.Fatalf("%d sends for a rule that is off", n)
+ }
+ if got := nf.outbox(t, id); len(got) != 0 {
+ t.Fatalf("outbox = %+v, want the row dropped", got)
+ }
+ // Back on, its rows go again.
+ if err := nf.st.SetAlertRule(alertScopeFleet, "disk_eta", alertRuleSetting{Enabled: true}); err != nil {
+ t.Fatal(err)
+ }
+ nf.queue(t, id, inc, alertKindFire, fireNote())
+ nf.drain(t)
+ if n := len(nf.hook.requests()); n != 1 {
+ t.Fatalf("%d sends once the rule is on again, want 1", n)
+ }
+}
+
func TestDrainSkipsRevokedOperatorQueued(t *testing.T) {
nf := newNotifyFixture(t)
const op = "ops-a@example.com"
diff --git a/internal/console/alertstore.go b/internal/console/alertstore.go
index 0bd23c1d2..c7c5a78f8 100644
--- a/internal/console/alertstore.go
+++ b/internal/console/alertstore.go
@@ -1005,7 +1005,9 @@ func (s *Store) AlertRuleSettings(scope string) (map[string]alertRuleSetting, er
// SetAlertRule replaces rule's settings and subscriptions in scope. Every
// connector must belong to scope: one scope's alerts never reach another's
-// connector.
+// connector. Switching a rule off also deletes its incidents' unsent rows
+// in the same transaction (ruling R-42): a rule that is off sends nothing,
+// including a fire still waiting on a retry.
func (s *Store) SetAlertRule(scope, rule string, st alertRuleSetting) error {
tx, err := s.db.Begin()
if err != nil {
@@ -1020,6 +1022,12 @@ func (s *Store) SetAlertRule(scope, rule string, st alertRuleSetting) error {
ON CONFLICT(scope, rule) DO UPDATE SET enabled = excluded.enabled, threshold = excluded.threshold`, scope, rule, st.Enabled, th); err != nil {
return err
}
+ if !st.Enabled {
+ if _, err := tx.Exec(`DELETE FROM alert_outbox WHERE sent_at IS NULL AND kind IN ('fire', 'clear', 'update', 'reminder')
+ AND incident_id IN (SELECT id FROM alert_incidents WHERE scope = ? AND rule = ?)`, scope, rule); err != nil {
+ return err
+ }
+ }
if _, err := tx.Exec(`DELETE FROM alert_subscriptions WHERE scope = ? AND rule = ?`, scope, rule); err != nil {
return err
}
@@ -1038,6 +1046,22 @@ ON CONFLICT(scope, rule) DO UPDATE SET enabled = excluded.enabled, threshold = e
return tx.Commit()
}
+// AlertIncidentRuleOff reports whether incident id's rule is switched off
+// in its scope, for the drain's check before it sends (ruling R-42). A rule
+// with no settings row is on; an incident no longer there is not off.
+func (s *Store) AlertIncidentRuleOff(id int64) (bool, error) {
+ var enabled sql.NullBool
+ err := s.db.QueryRow(`SELECT r.enabled FROM alert_incidents i
+ LEFT JOIN alert_rules r ON r.scope = i.scope AND r.rule = i.rule WHERE i.id = ?`, id).Scan(&enabled)
+ if errors.Is(err, sql.ErrNoRows) {
+ return false, nil
+ }
+ if err != nil {
+ return false, err
+ }
+ return enabled.Valid && !enabled.Bool, nil
+}
+
// ---- snoozes -----------------------------------------------------------------
// Snooze silences (scope, rule, subject) until until. A later snooze