:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(signals): prevent duplicate anomaly score event uuids (#122467)
posthog[bot]d054dc77727b
diff --git a/products/signals/backend/anomaly_scores/emission.py b/products/signals/backend/anomaly_scores/emission.pyindex bab05e30abe5..d775a0ba129b 100644--- a/products/signals/backend/anomaly_scores/emission.py+++ b/products/signals/backend/anomaly_scores/emission.py@@ -71,21 +71,25 @@ def score_events() -> list[dict[str, Any]]:"""One event per series and bucket that is later than the series' newest scored bucket."""events: list[dict[str, Any]] = []+ event_uuids: set[str] = set()for series in scores.series:through = scored_through.get(series.series_key)for point in series.points:if through is not None and point.bucket_start <= through:continue- events.append(- _event(- team_id=team_id,- insight_id=insight_id,- scores=scores,- series=series,- point=point,- scored_at=scored_at,- )+ event = _event(+ team_id=team_id,+ insight_id=insight_id,+ scores=scores,+ series=series,+ point=point,+ scored_at=scored_at,)+ # Capture rejects the whole batch on a repeated uuid, e.g. an hourly bucket repeated at a DST fall-back.+ if event["event_uuid"] in event_uuids:+ continue+ event_uuids.add(event["event_uuid"])+ events.append(event)return eventsdiff --git a/products/signals/backend/anomaly_scores/scoring.py b/products/signals/backend/anomaly_scores/scoring.pyindex c1dbf75ff556..c35a7d13dc34 100644--- a/products/signals/backend/anomaly_scores/scoring.py+++ b/products/signals/backend/anomaly_scores/scoring.py@@ -203,12 +203,17 @@ def score_insight(timezone = team.timezone_infoseries: list[ScoredSeries] = []+ key_counts: dict[str, int] = {}for result in cast(list[dict[str, Any]], calculation.result)[: limits.max_series]:buckets = [_parse_bucket(day, timezone) for day in result.get("days") or []]breakdown_value = _breakdown_label(result.get("breakdown_value"))+ key = series_key(insight.pk, _series_definition(scoring_query, result), breakdown_value)+ repeats = key_counts.get(key, 0)+ key_counts[key] = repeats + 1series.append(ScoredSeries(- series_key=series_key(insight.pk, _series_definition(scoring_query, result), breakdown_value),+ # Lines that differ only by name share a key. Event uuids come from the key, so each repeat needs its own.+ series_key=key if repeats == 0 else _short_hash({"key": key, "repeat": repeats}),label=str(result.get("label") or ""),breakdown_value=breakdown_value,points=score_series(diff --git a/products/signals/backend/test/test_anomaly_scores.py b/products/signals/backend/test/test_anomaly_scores.pyindex 1cb07f09c7cf..aa7e9bec54d8 100644--- a/products/signals/backend/test/test_anomaly_scores.py+++ b/products/signals/backend/test/test_anomaly_scores.py@@ -163,6 +163,18 @@ class TestScoreInsight(SimpleTestCase):assert filtered["signup"] == original["signup"]assert filtered["pageview"] != original["pageview"]+ def test_series_that_differ_only_by_name_get_unique_keys_and_the_first_keeps_its_key(self) -> None:+ single = self._keys(_insight([EventsNode(event="$pageview")]), [_result(0, "web")])+ named = self._keys(+ _insight(+ [EventsNode(event="$pageview", custom_name="web"), EventsNode(event="$pageview", custom_name="app")]+ ),+ [_result(0, "web"), _result(1, "app")],+ )++ assert named["web"] == single["web"]+ assert named["app"] != named["web"]+class TestTrendsQueryFor(SimpleTestCase):@parameterized.expand(diff --git a/products/signals/backend/test/test_anomaly_scoring_tick.py b/products/signals/backend/test/test_anomaly_scoring_tick.pyindex 42fbb44e22e6..76d7561939c2 100644--- a/products/signals/backend/test/test_anomaly_scoring_tick.py+++ b/products/signals/backend/test/test_anomaly_scoring_tick.py@@ -175,6 +175,23 @@ class TestScoreEvents(SimpleTestCase):("new", start.isoformat()) for start in starts]+ def test_a_bucket_repeated_at_a_dst_fall_back_is_emitted_once(self) -> None:+ # Trends returns the 01:00 bucket twice on the fall-back night, and both parse to the same instant.+ starts = [datetime(2026, 11, 1, 0, tzinfo=NEW_YORK), datetime(2026, 11, 1, 1, tzinfo=NEW_YORK)]+ starts.append(starts[-1])++ events = score_events(+ team_id=1,+ insight_id=7,+ scores=_scores(_series("hourly", starts)),+ scored_through={},+ scored_at=datetime(2026, 11, 2, tzinfo=UTC),+ )++ assert [event["properties"]["$anomaly_bucket_start"] for event in events] == [+ start.isoformat() for start in starts[:2]+ ]+def test_the_event_carries_the_bucket_start_and_null_score_and_flag_during_warm_up(self) -> None:start = datetime(2026, 10, 19, tzinfo=NEW_YORK)series = ScoredSeries(