From 7f04ef62ef82f355639a35e96893de5e099f3974 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sun, 8 Nov 2026 16:44:37 -0800 Subject: [PATCH] fix(alert): keep a newer email failure visible on the rule Dropping every recipient of an old notification cleared the rule's email status even when a newer notification had set it. Clear it only while it still reads retrying. Also make the refusal for creating a user accurate in every identity provider mode, including the one where nothing provisions accounts. --- internal/alert/email.go | 8 +++-- internal/alert/email_test.go | 39 ++++++++++++++++++++++++ internal/api/users.go | 2 +- internal/db/generated/alert_email.sql.go | 9 ++++++ internal/db/queries/alert_email.sql | 3 ++ 5 files changed, 58 insertions(+), 3 deletions(-) diff --git a/internal/alert/email.go b/internal/alert/email.go index 54249d06..1e752b01 100644 --- a/internal/alert/email.go +++ b/internal/alert/email.go @@ -522,8 +522,12 @@ func (e *Engine) settleEmail(ctx context.Context, row generated.AlertEmailDelive return } if len(rows) == 0 { - // Every recipient was dropped, or the rule went with them. - e.setRuleEmailStatus(ctx, row.RuleID, "", "") + // Every recipient was dropped, or the rule went with them. Only this + // notification's own retry state is cleared; any other status belongs + // to a newer notification. + if err := e.store.q.ClearRetryingRuleEmailStatus(ctx, row.RuleID); err != nil { + slog.Error("alert: clear rule email status", "rule", row.RuleID, "err", err) + } return } var failed, suppressed int diff --git a/internal/alert/email_test.go b/internal/alert/email_test.go index ca5a223f..23b90306 100644 --- a/internal/alert/email_test.go +++ b/internal/alert/email_test.go @@ -520,6 +520,45 @@ func TestRetryDropsARecipientNoLongerOnTheRule(t *testing.T) { } } +func TestDroppingEveryRecipientClearsOnlyItsOwnRetryStatus(t *testing.T) { + setup := func(t *testing.T) (*mailHarness, Rule) { + h := newMailHarness(t, 30) + rule := h.rule(t, "errors", "gone@example.com", "keep@example.com") + h.box.fail = func(auth.AlertMail) error { return errors.New("451 later") } + h.fire(rule) + h.drain() + h.box.fail = nil + if status := h.ruleStatus(t, rule.ID).EmailStatus; status != "retrying" { + t.Fatalf("rule status = %q", status) + } + rule.EmailRecipients = []string{"new@example.com"} + if _, err := h.store.ReplaceRule(rule); err != nil { + t.Fatal(err) + } + return h, rule + } + + t.Run("retrying status is cleared", func(t *testing.T) { + h, rule := setup(t) + h.now = h.now.Add(2 * time.Minute) + h.drain() + if status := h.ruleStatus(t, rule.ID).EmailStatus; status != "" { + t.Fatalf("rule status = %q", status) + } + }) + + t.Run("a newer failure stays visible", func(t *testing.T) { + h, rule := setup(t) + h.engine.setRuleEmailStatus(context.Background(), rule.ID, "failed", "newer notification") + h.now = h.now.Add(2 * time.Minute) + h.drain() + stored := h.ruleStatus(t, rule.ID) + if stored.EmailStatus != "failed" || stored.EmailStatusDetail != "newer notification" { + t.Fatalf("rule = %+v", stored) + } + }) +} + func TestEmailWorkerKeepsRunningAfterAPanic(t *testing.T) { h := newMailHarness(t, 30) h.engine.now = nil diff --git a/internal/api/users.go b/internal/api/users.go index 21c5f5b3..b0c86a97 100644 --- a/internal/api/users.go +++ b/internal/api/users.go @@ -112,7 +112,7 @@ func (h *UserHandler) ListUsers(c *echo.Context) error { // CreateUser adds a new user (admin only). func (h *UserHandler) CreateUser(c *echo.Context) error { if h.ownership != accountOwnershipLocal { - return ownedByIdentityProvider("Accounts are created by the identity provider, not in Fanout.") + return ownedByIdentityProvider("Accounts cannot be created in Fanout when roles come from the identity provider.") } var req struct { Email string `json:"email"` diff --git a/internal/db/generated/alert_email.sql.go b/internal/db/generated/alert_email.sql.go index 52015308..cd19a6c2 100644 --- a/internal/db/generated/alert_email.sql.go +++ b/internal/db/generated/alert_email.sql.go @@ -22,6 +22,15 @@ func (q *Queries) ClaimAlertEmailDelivery(ctx context.Context, id string) (int64 return result.RowsAffected() } +const clearRetryingRuleEmailStatus = `-- name: ClearRetryingRuleEmailStatus :exec +UPDATE alert_rules SET email_status = '', email_status_detail = '', email_status_at = '' WHERE id = ? AND email_status = 'retrying' +` + +func (q *Queries) ClearRetryingRuleEmailStatus(ctx context.Context, id string) error { + _, err := q.db.ExecContext(ctx, clearRetryingRuleEmailStatus, id) + return err +} + const countAlertEmailSendsSince = `-- name: CountAlertEmailSendsSince :one SELECT COUNT(*) FROM alert_email_sends WHERE recipient = ? AND sent_at >= ? ` diff --git a/internal/db/queries/alert_email.sql b/internal/db/queries/alert_email.sql index 07ce4576..be47503a 100644 --- a/internal/db/queries/alert_email.sql +++ b/internal/db/queries/alert_email.sql @@ -41,3 +41,6 @@ DELETE FROM alert_email_deliveries WHERE status IN ('sent', 'failed', 'suppresse -- name: UpdateRuleEmailStatus :exec UPDATE alert_rules SET email_status = ?, email_status_detail = ?, email_status_at = ? WHERE id = ?; + +-- name: ClearRetryingRuleEmailStatus :exec +UPDATE alert_rules SET email_status = '', email_status_detail = '', email_status_at = '' WHERE id = ? AND email_status = 'retrying';