Files
Pulse/internal/ai/findings_flapping_test.go
rcourtman a10c175f31 Fold alert-mirroring findings, collapse flapping, surface lasting decisions
Two users (discussions #1623, #1699) could not find Patrol's durable
outcomes: the Needs attention detail offered only Acknowledge and
Suppress, and Remember as expected, Dismiss: Not an issue, Dismiss:
Later, and Create rule sat two levels below it. The same screenshot
showed an alert with eleven open/resolved transitions in a day listed as
eleven timeline rows, and Patrol findings restating an active alert
appearing beside it. Telemetry shows most findings are seen and ignored.

Backend: findings_alert_mirror.go is the single matcher for a finding
that restates an active alert (explicit alert identifier, or same
canonical resource plus the same condition class). Patrol stamps
mirrors_alert_id/mirrors_alert_type after every real and demo cycle and
clears them when the alert resolves; the finding is kept, not dropped.
The storm throttler now also owns flap detection: four or more
open/resolved transitions in 24h mark a finding flapping, and the store
maintains one collapsed lifecycle row with the count instead of a row per
transition, hydrating the window from persisted lifecycle after restart.
The attention projection applies the same threshold to alert timelines
and exposes a flapping summary while keeping the full timeline.

Frontend: the attention detail gains a Lasting decisions section that
offers the four Patrol decisions on the finding mirroring the alert, each
with one line saying what it does and how long it lasts, an inline
confirmation, and a remembered-decision state with Reopen. An alert with
no mirrored finding says Patrol has nothing to remember and points at
alert thresholds. Flapping items carry one label and a summarised
timeline with every transition under an expansion. FindingsPanel demotes
active alert-mirroring findings into a collapsed group. The Patrol
surface loads dismissed findings so remembered decisions are visible.
2026-09-02 07:02:51 +01:00

163 lines
5.3 KiB
Go

package ai
import (
"testing"
"time"
)
// flapFinding builds a fresh warning finding for the flap tests.
func flapFinding() *Finding {
return &Finding{
ID: "docker:h/web:exited",
Key: "container-exited",
Severity: FindingSeverityWarning,
Category: FindingCategoryReliability,
ResourceID: "docker:h/web",
ResourceName: "web",
Title: `Container "web" exited`,
}
}
func lifecycleTypes(f *Finding) []string {
types := make([]string, 0, len(f.Lifecycle))
for _, event := range f.Lifecycle {
types = append(types, event.Type)
}
return types
}
func TestFindingsStore_CollapsesFlappingIntoOneLifecycleRow(t *testing.T) {
store := NewFindingsStore()
store.SetStormThrottler(newFindingStormThrottler())
store.Add(flapFinding())
// Resolve and re-detect repeatedly. Each resolve and each regression is
// one open/resolved transition.
for i := 0; i < 6; i++ {
if !store.ResolveWithReason("docker:h/web:exited", "container running again") {
t.Fatalf("resolve %d failed", i)
}
if store.Add(flapFinding()) {
t.Fatalf("re-detection %d was treated as a new finding", i)
}
}
f := store.Get("docker:h/web:exited")
if f == nil {
t.Fatal("finding disappeared")
}
if f.Flapping == nil {
t.Fatalf("twelve transitions in a minute were not labelled flapping: %v", lifecycleTypes(f))
}
if f.Flapping.TransitionCount != 12 {
t.Fatalf("TransitionCount = %d, want 12", f.Flapping.TransitionCount)
}
if f.RegressionCount != 6 {
t.Fatalf("RegressionCount = %d, want 6 (regressions still counted while collapsed)", f.RegressionCount)
}
// Transitions 1-3 were recorded individually; from the fourth onward the
// store maintains a single flapping row instead of appending nine more.
types := lifecycleTypes(f)
flappingRows := 0
individualTransitions := 0
for _, typ := range types {
switch typ {
case FindingLifecycleFlapping:
flappingRows++
case "regressed", "auto_resolved", "resolved":
individualTransitions++
}
}
if flappingRows != 1 {
t.Fatalf("flapping rows = %d, want exactly one collapsed row: %v", flappingRows, types)
}
if individualTransitions != findingFlapThreshold-1 {
t.Fatalf("individual transition rows = %d, want %d pre-label rows: %v", individualTransitions, findingFlapThreshold-1, types)
}
last := f.Lifecycle[len(f.Lifecycle)-1]
if last.Type != FindingLifecycleFlapping {
t.Fatalf("last lifecycle row = %q, want the collapsed flapping row", last.Type)
}
if last.Metadata["transition_count"] != "12" {
t.Fatalf("collapsed row transition_count = %q, want 12", last.Metadata["transition_count"])
}
if last.Metadata["last_transition"] != "regressed" {
t.Fatalf("collapsed row last_transition = %q, want regressed", last.Metadata["last_transition"])
}
}
func TestFindingsStore_WithoutThrottlerKeepsIndividualRows(t *testing.T) {
store := NewFindingsStore()
store.Add(flapFinding())
for i := 0; i < 3; i++ {
store.ResolveWithReason("docker:h/web:exited", "running again")
store.Add(flapFinding())
}
f := store.Get("docker:h/web:exited")
if f.Flapping != nil {
t.Fatal("no throttler installed but the finding was labelled flapping")
}
for _, typ := range lifecycleTypes(f) {
if typ == FindingLifecycleFlapping {
t.Fatalf("collapsed row appeared without a throttler: %v", lifecycleTypes(f))
}
}
}
func TestFindingsStore_FlappingLabelClearsWhenChurnStops(t *testing.T) {
store := NewFindingsStore()
throttler := newFindingStormThrottler()
store.SetStormThrottler(throttler)
store.Add(flapFinding())
for i := 0; i < 3; i++ {
store.ResolveWithReason("docker:h/web:exited", "running again")
store.Add(flapFinding())
}
if store.Get("docker:h/web:exited").Flapping == nil {
t.Fatal("expected flapping after six transitions")
}
// Age the tracked window out, then one more transition: no longer flapping.
throttler.mu.Lock()
tracker := throttler.flaps["docker:h/web:exited"]
for i := range tracker.transitions {
tracker.transitions[i] = tracker.transitions[i].Add(-findingFlapWindow - time.Hour)
}
throttler.mu.Unlock()
store.ResolveWithReason("docker:h/web:exited", "running again")
f := store.Get("docker:h/web:exited")
if f.Flapping != nil {
t.Fatalf("label persisted after the window emptied: %+v", f.Flapping)
}
if last := f.Lifecycle[len(f.Lifecycle)-1]; last.Type != "auto_resolved" {
t.Fatalf("post-flap transition row = %q, want an ordinary auto_resolved row", last.Type)
}
}
func TestFindingJSONRoundTripsFlappingAndMirror(t *testing.T) {
now := time.Date(2026, 8, 10, 12, 0, 0, 0, time.UTC)
f := flapFinding()
f.DetectedAt = now
f.LastSeenAt = now
f.Flapping = &FindingFlapping{TransitionCount: 7, WindowHours: 24, FirstTransitionAt: now.Add(-20 * time.Hour), LastTransitionAt: now}
f.MirrorsAlertID = "docker-container-state-docker:h/web"
f.MirrorsAlertType = "docker-container-state"
data, err := f.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var decoded Finding
if err := decoded.UnmarshalJSON(data); err != nil {
t.Fatal(err)
}
if decoded.Flapping == nil || decoded.Flapping.TransitionCount != 7 || decoded.Flapping.WindowHours != 24 {
t.Fatalf("flapping did not round-trip: %+v", decoded.Flapping)
}
if decoded.MirrorsAlertID != f.MirrorsAlertID || decoded.MirrorsAlertType != f.MirrorsAlertType {
t.Fatalf("mirror fields did not round-trip: %q %q", decoded.MirrorsAlertID, decoded.MirrorsAlertType)
}
}