console: a flapping incident holds an hour however its members leave (R-39)
When a flapping incident's firing members run out, by clearing or by going gone, it enters its quiet hold and closes an hour later; gone members are dropped. The reconstruction of the hold from recent fire timestamps is removed. The review's A/B timeline is a test, and the step simulation checks that a flapping incident closes only an hour after the last firing signal for its (scope, rule).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+65
−25
internal/console/alertengine.go+9 −18internal/console/alertengine_test.go+53 −5internal/console/alertstore.go+3 −2
diff --git a/internal/console/alertengine.go b/internal/console/alertengine.go| index 65ca0d60f..470753bd3 100644 |
| --- a/internal/console/alertengine.go |
| +++ b/internal/console/alertengine.go |
| @@ -13,10 +13,13 @@ package console |
| // and the history say what cleared. A key with no signal in a pass keeps |
| // its state, because some sources report only every 5th pass. |
| // |
| -// A flapping incident (§6.7) does not close when its last member leaves. |
| -// It goes quiet: it keeps the members that left, and LastSeenAt records |
| +// A flapping incident (§6.7) does not close when its last member leaves, |
| +// whether it stopped firing or went gone (ruling R-39). It goes quiet: it |
| +// keeps the members that left and are not gone, and LastSeenAt records |
| // when (alertIncident.quiet). A firing signal wakes it with just the |
| -// subjects firing now; an hour quiet closes it. |
| +// subjects firing now; an hour quiet closes it. The hold is that recorded |
| +// time, never worked out again from the fire history, so no way of leaving |
| +// can shorten it. |
| // |
| // The message clocks: a fire or reminder sets LastReminderAt, an update or |
| // reminder sets LastUpdateAt. A reminder is due 24 hours after the later |
| @@ -186,12 +189,10 @@ func (st incidentStep) advance(in alertIncident, keys []alertKey) (out alertInci |
| kept := slices.Clone(in.Members) // members not gone, for a quiet hold |
| var joined []string |
| anyGone := false |
| - goneNow := map[string]bool{} |
| for _, k := range keys { |
| sg := st.latest[k] |
| switch { |
| case sg.Gone: |
| - goneNow[k.Subject] = true |
| anyGone = anyGone || slices.Contains(in.Members, k.Subject) |
| firingNow = slices.DeleteFunc(firingNow, func(m string) bool { return m == k.Subject }) |
| kept = slices.DeleteFunc(kept, func(m string) bool { return m == k.Subject }) |
| @@ -206,18 +207,6 @@ func (st incidentStep) advance(in alertIncident, keys []alertKey) (out alertInci |
| firingNow = slices.DeleteFunc(firingNow, func(m string) bool { return m == k.Subject }) |
| } |
| } |
| - if out.Flapping && len(firingNow) == 0 && len(kept) == 0 { |
| - // The last firing member left by going gone, but one that stopped |
| - // firing earlier may still be flapping: anything that fired inside |
| - // the window keeps the hold (§6.7). |
| - for k := range st.recentFires { |
| - if k.Scope == in.Scope && k.Rule == in.Rule && !goneNow[k.Subject] && st.recent(k) > 0 { |
| - kept = append(kept, k.Subject) |
| - } |
| - } |
| - slices.Sort(kept) |
| - } |
| - |
| if len(firingNow) == 0 && !quiet { |
| out.LastSeenAt = now // the last member left in this pass |
| } |
| @@ -226,7 +215,9 @@ func (st incidentStep) advance(in alertIncident, keys []alertKey) (out alertInci |
| case len(firingNow) > 0: |
| out.Members = firingNow |
| out.LastSeenAt = time.Time{} |
| - case out.Flapping && len(kept) > 0: |
| + case out.Flapping: |
| + // Held, even with every member gone (ruling R-39): gone members |
| + // are dropped, and the hour runs from LastSeenAt. |
| out.Members = kept |
| closing = !now.Before(out.LastSeenAt.Add(alertFlapHold)) |
| default: |
diff --git a/internal/console/alertengine_test.go b/internal/console/alertengine_test.go| index bc140119c..bb7787e75 100644 |
| --- a/internal/console/alertengine_test.go |
| +++ b/internal/console/alertengine_test.go |
| @@ -350,6 +350,37 @@ func TestStepFlappingSurvivesGone(t *testing.T) { |
| } |
| } |
| +// Ruling R-39, the whole-branch review's timeline: A flaps, then fires for |
| +// over an hour beside B. A stops while B still fires, so A leaves the |
| +// incident; a minute later B goes gone. Nothing of A's last hour of fires |
| +// is left to reconstruct the hold from, yet A has been clear for a minute |
| +// only: the incident holds for an hour from when its last member left, |
| +// and B, gone, is simply dropped. |
| +func TestStepFlappingHoldSurvivesGoneAfterLongFire(t *testing.T) { |
| + sim := newStepSim(t) |
| + sim.run(0, 142, func(m int) []alertSignal { |
| + a := quiet("a") |
| + if m == 0 || m == 2 || (m >= 4 && m < 80 && m != 5) { |
| + a = firing("a") |
| + } |
| + b := quiet("b") |
| + switch { |
| + case m >= 81: |
| + b = gone("b") |
| + case m >= 10: |
| + b = firing("b") |
| + } |
| + if m == 140 && (len(sim.open) != 1 || !sim.open[0].quiet()) { |
| + t.Fatalf("minute 140: open %+v, want the incident still in its quiet hold", sim.open) |
| + } |
| + return []alertSignal{a, b} |
| + }) |
| + sim.emitsAre(simWant{9, "fire", []string{"a"}}, simWant{141, "clear", nil}) |
| + if len(sim.open) != 0 { |
| + t.Fatalf("open at minute 141: %+v, want it closed an hour after B left", sim.open) |
| + } |
| +} |
| + |
| func TestStepDuplicateOpenClosed(t *testing.T) { |
| sim := newStepSim(t) |
| a := alertIncident{ID: 1, Scope: "fleet", Rule: "disk_eta", Level: "warning", |
| @@ -429,9 +460,15 @@ func TestStepGoneCloses(t *testing.T) { |
| t.Fatalf("open = %+v", sim.open) |
| } |
| }) |
| - t.Run("gone ends a flapping hold", func(t *testing.T) { |
| + t.Run("gone does not end a flapping hold", func(t *testing.T) { |
| + // Ruling R-39: b1 went quiet at minute 5, so the hold runs to |
| + // minute 65; going gone at 12 drops it from the members and |
| + // shortens nothing. |
| sim := newStepSim(t) |
| - sim.run(0, 15, func(m int) []alertSignal { |
| + sim.run(0, 66, func(m int) []alertSignal { |
| + if m == 64 && len(sim.open) != 1 { |
| + t.Fatalf("minute 64: open %+v, want the hold", sim.open) |
| + } |
| switch { |
| case m == 0 || m == 2 || m == 4: |
| return []alertSignal{firing("b1")} |
| @@ -441,9 +478,9 @@ func TestStepGoneCloses(t *testing.T) { |
| return []alertSignal{gone("b1")} |
| } |
| }) |
| - sim.emitsAre(simWant{9, "fire", []string{"b1"}}, simWant{12, "clear", []string{"b1"}}) |
| - if r := sim.emits[1].Incident.CloseReason; r != alertCloseGone { |
| - t.Fatalf("close reason = %q, want %q", r, alertCloseGone) |
| + sim.emitsAre(simWant{9, "fire", []string{"b1"}}, simWant{65, "clear", nil}) |
| + if len(sim.open) != 0 { |
| + t.Fatalf("open = %+v", sim.open) |
| } |
| }) |
| } |
| @@ -595,6 +632,9 @@ func runStepInvariants(t *testing.T, seed uint64, steps int, subjects []string) |
| lastFireOrReminder := map[int64]time.Time{} |
| lastUpdateOrReminder := map[int64]time.Time{} |
| cleared := map[int64]bool{} |
| + // lastFiring is the last pass with a firing signal for the group: a |
| + // flapping incident closes only an hour after it (ruling R-39). |
| + var lastFiring time.Time |
| for m := range steps { |
| now := minute(m) |
| @@ -627,6 +667,9 @@ func runStepInvariants(t *testing.T, seed uint64, steps int, subjects []string) |
| } |
| changed, emits := sim.step(now, sigs...) // (h) is checked in step |
| + if slices.ContainsFunc(sigs, func(sg alertSignal) bool { return sg.Firing && !sg.Gone }) { |
| + lastFiring = now |
| + } |
| // (a) one open incident per (scope, rule). |
| if len(sim.open) > 1 { |
| @@ -682,6 +725,11 @@ func runStepInvariants(t *testing.T, seed uint64, steps int, subjects []string) |
| continue |
| } |
| closing[c.ID] = true |
| + // (j) a flapping incident closes only after an hour with no |
| + // firing signal for its (scope, rule), however its members left. |
| + if c.Flapping && c.CloseReason != alertCloseDuplicate && now.Sub(lastFiring) < alertFlapHold { |
| + return fmt.Errorf("minute %d: flapping incident %d closed %v after the last firing signal", m, c.ID, now.Sub(lastFiring)) |
| + } |
| want := 0 |
| if notified[c.ID] { |
| want = 1 |
diff --git a/internal/console/alertstore.go b/internal/console/alertstore.go| index 24173e22c..0bd23c1d2 100644 |
| --- a/internal/console/alertstore.go |
| +++ b/internal/console/alertstore.go |
| @@ -214,8 +214,9 @@ type alertRuleSetting struct { |
| // (alertengine.go) gives two fields a meaning of their own: |
| // |
| // - Members is the subjects firing now, in join order. On a quiet |
| -// flapping incident it is the set held for the quiet hour, and on a |
| -// closed row the members it had when it closed. |
| +// flapping incident it is the members that left and are not gone, |
| +// none if every one went gone (ruling R-39), and on a closed row the |
| +// members it had when it closed. |
| // - LastSeenAt is zero while any member fires. On a flapping incident it |
| // is the time it went quiet, and on a closed row when it stopped. |
| // |