: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(alerts): stop a due time past the anchor skipping a check (#109554)
Jimmy Zhuc2a000602b2d
diff --git a/products/alerts/backend/facade/scheduling.py b/products/alerts/backend/facade/scheduling.pyindex eb304cf7eda0..9dbb56b4d940 100644--- a/products/alerts/backend/facade/scheduling.py+++ b/products/alerts/backend/facade/scheduling.py@@ -214,12 +214,18 @@ def _next_calendar_schedule_start_time() -> datetime:earliest_allowed = nowif next_check_at is not None:+ # `next_check_at` is when the check became due, which is not always the anchor: enabling+ # an alert or changing its threshold sets it to now, and a spring-forward date shifts it+ # past an anchor that does not exist. Adding the interval to a due time later in the+ # period than the anchor puts the bound past the next anchor, which skips a whole period.+ # So the interval is added to the anchor the due time belongs to.+ previous_anchor_local = datetime.combine(+ next_check_at.astimezone(team_timezone).date(),+ first_candidate_local.time(),+ )earliest_allowed = max(earliest_allowed,- _localize_wall_time(- team_timezone,- (next_check_at.astimezone(team_timezone) + interval_delta).replace(tzinfo=None),- ).astimezone(UTC),+ _localize_wall_time(team_timezone, previous_anchor_local + interval_delta).astimezone(UTC),)candidate_local = first_candidate_localdiff --git a/products/alerts/backend/test/test_scheduling.py b/products/alerts/backend/test/test_scheduling.pyindex 362ad6948af5..2474468e45e2 100644--- a/products/alerts/backend/test/test_scheduling.py+++ b/products/alerts/backend/test/test_scheduling.py@@ -275,6 +275,40 @@ class TestNextCalendarCheckTime:assert (result.year, result.month, result.day, result.hour, result.minute) == expected_utcassert result.tzinfo is not None+ @parameterized.expand(+ [+ # Enabling an alert or changing its threshold sets next_check_at to now, so a due+ # time later in the day than the anchor is the common case, not an edge one.+ (+ "due_after_the_anchor",+ "UTC",+ "09:35",+ datetime(2026, 3, 19, 9, 40, tzinfo=UTC),+ datetime(2026, 3, 20, 9, 35, tzinfo=UTC),+ ),+ # 02:30 does not exist on 2026-03-08 in America/New_York, so that day's check runs+ # at 03:30 local and the due time carries the shift.+ (+ "due_shifted_by_spring_forward",+ "America/New_York",+ "02:30",+ datetime(2026, 3, 8, 7, 30, tzinfo=UTC),+ datetime(2026, 3, 9, 6, 30, tzinfo=UTC),+ ),+ ]+ )+ def test_a_due_time_past_the_anchor_does_not_skip_a_period(+ self, _name: str, tz_name: str, anchor: str, due_at: datetime, expected: datetime+ ) -> None:+ result = next_calendar_check_time(+ CalendarInterval.DAILY,+ now=due_at,+ tz_name=tz_name,+ next_check_at=due_at,+ schedule_start_time=anchor,+ )+ assert result == expected+def test_daily_across_dst_spring_forward(self) -> None:# US spring-forward was 2026-03-08: local 1am tomorrow maps PST(-8) -> PDT(-7),# so the UTC anchor shifts from 09:00 to 08:00 across the transition.