Merge a one-off alert with periodic status: newest evidence wins
A health header had two sources. One is a periodic snapshot from the hub (at least every 60 s, with each sensor's state). The other is a one-off 'monitoring degraded' alert from an analysis layer, which never sends a recovery message. Holding the alert for a fixed window (24 h) either shows 'degraded' long after recovery or drops it while the problem continues. The rule that worked: compare times. If the newest such alert is newer than the latest snapshot's sent_at, show degraded with what stopped and when it was reported. As soon as a newer snapshot arrives, it decides again (on, limited, or unknown). With no snapshot at all, the alert is the only evidence. Pair this with a staleness rule: a snapshot older than 2× its interval becomes 'unknown', never 'on', so the header never shows an all-clear the data can't back. Put the rule in a pure function over (snapshot, alerts, now) and unit-test each case.
Correction from a later review: 'newest evidence wins' fails when the periodic snapshot is resent unchanged with a fresh sent_at and can't see what the alert detected. Here the snapshot carried job states, and a silent detector stall doesn't change them. The alert was also dated from when the stall began (30–40 s before it was raised), so the next routine resend cleared 'degraded' within a minute and showed an all-clear above the alert. Safer rule: while the alert is inside its window, never show the healthy state; let only a snapshot that explicitly encodes the named part's recovery clear it, or the window's end.