Skip to content

: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.

chore(alerts): update product alerting scheduling skill (#71771)

Matt Puafecf4dbcf2df
diff --git a/.agents/skills/adding-product-alerting/SKILL.md b/.agents/skills/adding-product-alerting/SKILL.md
index d30bb2f12d79..4d1fa7c449ae 100644
--- a/.agents/skills/adding-product-alerting/SKILL.md
+++ b/.agents/skills/adding-product-alerting/SKILL.md
@@ -3,8 +3,8 @@ name: adding-product-alerting
description: >
Recommended repo-engineering guide when adding alerting to a PostHog product or
extending the shared alerts platform. Routes lifecycle state machines,
- AlertPolicy, destinations, HogFunction dispatch, email, fixed-cadence scheduling,
- insight evaluation, and the AlertWizard. Use for product alert implementations,
+ AlertPolicy, destinations, HogFunction dispatch, email, fixed-cadence and calendar
+ scheduling, insight evaluation, and the AlertWizard. Use for product alert implementations,
shared destination types, lifecycle or scheduling options, advanced alert settings,
and platform alert infrastructure. Not for configuring alerts in an existing product.
---
@@ -39,13 +39,13 @@ Both paths must preserve these rules:
3. **One product mutator.** Every persisted `state` or `consecutive_failures` write goes through the product adapter's `apply_outcome`.
4. **Dispatch and persistence agree.** For HogFunction notifications, do not persist a notification-dependent transition until the internal-event producer acknowledges the event. Restore the pre-check outcome when production fails. This acknowledgement does not confirm downstream destination execution.
5. **Destinations are allowlisted.** Shared support does not automatically expose a destination in every product.
-6. **Scheduling math is shared, eligibility is product-owned.** Reuse fixed-cadence math, but keep model-specific due predicates and calendar scheduling with the adopter.
+6. **Scheduling math is shared, eligibility is product-owned.** Reuse fixed-cadence, calendar-anchor, timezone, and schedule-restriction helpers from `products/alerts/backend/scheduling.py`. Keep model-specific due predicates and persistence with the adopter.
7. **Shared code has no product branches.** `common/alerting/` stays pure Python. Reusable Django behavior belongs in `products/alerts/backend/`.
8. **Defaults remain backward compatible.** New platform options must preserve existing adopters until they explicitly opt in.
## Current limits
-There is no generic alert base model, product registry, push-mode `submit_check(...)`, generic Temporal harness, or shared calendar scheduler. Do not invent a parallel framework around those missing pieces. For non-insight products, keep evaluation, persistence, due queries, history, and orchestration in the product until a shared contract lands.
+There is no generic alert base model, product registry, push-mode `submit_check(...)`, generic scheduler runner, or generic Temporal harness. Do not invent a parallel framework around those missing pieces. For non-insight products, keep evaluation, persistence, due queries, history, and orchestration in the product until a shared contract lands.
## Reference appendix
diff --git a/.agents/skills/adding-product-alerting/references/adopting-platform-alerting.md b/.agents/skills/adding-product-alerting/references/adopting-platform-alerting.md
index 8096c37e4173..e3cdacb2f329 100644
--- a/.agents/skills/adding-product-alerting/references/adopting-platform-alerting.md
+++ b/.agents/skills/adding-product-alerting/references/adopting-platform-alerting.md
@@ -71,6 +71,23 @@ For email, call `send_alert_email(...)` through the facade. The product must cho
## 6. Add scheduling and due selection
+Import the product-facing scheduling contract directly from its implementation module:
+
+```python
+from products.alerts.backend.scheduling import (
+ CalendarInterval,
+ advance_next_check_at,
+ compute_shard_offset_seconds,
+ is_utc_datetime_blocked,
+ is_weekend,
+ next_calendar_check_time,
+ parse_blocked_windows_tuples,
+ scan_next_unblocked_utc,
+ to_calendar_interval,
+ validate_and_normalize_schedule_restriction,
+)
+```
+
For fixed-minute checks:
- Reuse `compute_shard_offset_seconds(...)` and `advance_next_check_at(...)`.
@@ -78,7 +95,12 @@ For fixed-minute checks:
- Keep one stable shard calculation across create, update, and evaluation paths.
- Implement and test a product-specific due predicate with team scoping, enabled state, broken state, snooze state, and `next_check_at` behavior.
-For calendar scheduling, keep timezone anchors and restrictions in the product. Do not mix calendar semantics into the fixed-cadence helpers.
+For calendar-aligned checks:
+
+- Convert product interval values with `to_calendar_interval(...)`, then use `next_calendar_check_time(...)` with the team's IANA timezone.
+- Validate stored quiet-hour payloads with `validate_and_normalize_schedule_restriction(...)` and parse them with `parse_blocked_windows_tuples(...)`.
+- Use `scan_next_unblocked_utc(...)`, `is_utc_datetime_blocked(...)`, and `is_weekend(...)` instead of recreating timezone or DST logic.
+- Keep model access, API error translation, bounded-retry logging, and `next_check_at` persistence in the product adapter.
## 7. Add product orchestration
diff --git a/.agents/skills/adding-product-alerting/references/architecture.md b/.agents/skills/adding-product-alerting/references/architecture.md
index 1f4f9073e157..30370376ae12 100644
--- a/.agents/skills/adding-product-alerting/references/architecture.md
+++ b/.agents/skills/adding-product-alerting/references/architecture.md
@@ -4,17 +4,17 @@ Use this reference to decide where code belongs before editing it.
## Layer map
-| Layer | Location | Owns |
-| ---------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
-| Pure lifecycle and cadence math | `common/alerting/` | State transitions, policy decisions, notification actions, fixed-minute cadence and sharding |
-| Shared Django alert infrastructure | `products/alerts/backend/` | Destination configuration and persistence, internal-event delivery, email transport, insight alert models/API, and insight evaluation |
-| Product adapter | `products/<name>/backend/` | Domain evaluation, model snapshots, the single mutator, event payloads, allowed destinations, due queries, history, and orchestration |
-| Shared alert creation UI | `frontend/src/lib/components/Alerting/AlertWizard/` | Reusable HogFunction destination, trigger, and configuration flow |
-| Product UI | `products/<name>/frontend/` or `frontend/src/scenes/<name>/` | Alert settings, product-specific fields, entry points, detail pages, and wizard configuration |
+| Layer | Location | Owns |
+| --------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| Pure lifecycle decisions | `common/alerting/` | State transitions, policy decisions, and notification actions |
+| Shared alert infrastructure | `products/alerts/backend/` | Scheduling math, destination configuration and persistence, internal-event delivery, email transport, insight alert models/API, and insight evaluation |
+| Product adapter | `products/<name>/backend/` | Domain evaluation, model snapshots, the single mutator, event payloads, allowed destinations, due queries, history, and orchestration |
+| Shared alert creation UI | `frontend/src/lib/components/Alerting/AlertWizard/` | Reusable HogFunction destination, trigger, and configuration flow |
+| Product UI | `products/<name>/frontend/` or `frontend/src/scenes/<name>/` | Alert settings, product-specific fields, entry points, detail pages, and wizard configuration |
`products/logs` is the reference adopter for the shared lifecycle, fixed-cadence scheduling, HogFunction destinations, delivery rollback, and product-owned Temporal orchestration.
-Insight alerts are the reference for email delivery and calendar scheduling. Their model, API, and query evaluation live in `products/alerts/backend/` and `posthog/tasks/alerts/`. The evaluation package is shared across insight query kinds, but it is not a generic evaluator for unrelated products.
+Insight alerts are the reference adopter for shared calendar anchors, schedule restrictions, weekend skipping, and email delivery. Their model, API, query evaluation, and Django scheduling adapters live in `products/alerts/backend/` and `posthog/tasks/alerts/`. The evaluation package is shared across insight query kinds, but it is not a generic evaluator for unrelated products.
## Lifecycle contract
@@ -67,14 +67,18 @@ Email callers use `send_alert_email(...)` through the facade. The caller owns re
## Scheduling contract
-`common/alerting/scheduling.py` supports fixed-minute cadences:
+`products/alerts/backend/scheduling.py` is pure Python and owns reusable scheduling math:
- `compute_shard_offset_seconds(...)` deterministically assigns a UUID-keyed alert to scheduler ticks.
- `advance_next_check_at(...)` advances from the prior schedule, skips missed intervals, snaps to the midnight-anchored cadence grid, and applies the shard offset.
+- `CalendarInterval` and `to_calendar_interval(...)` define the product-facing interval contract.
+- `next_calendar_check_time(...)` computes fixed-cadence or team-local daily, weekly, and monthly anchors while preserving wall-clock behavior across DST changes.
+- `validate_and_normalize_schedule_restriction(...)` and `parse_blocked_windows_tuples(...)` validate product payloads and produce the pure blocked-window contract.
+- `scan_next_unblocked_utc(...)`, `is_utc_datetime_blocked(...)`, and `is_weekend(...)` apply timezone-aware restrictions without importing Django models.
Use the scheduler's real interval when computing shards. Use the same shard function when creating, updating, and advancing an alert.
-Due eligibility stays product-specific because model fields, states, snooze behavior, and tenant constraints differ. Calendar behavior such as team-local anchors, schedule restrictions, and weekend skipping remains separate from fixed-grid scheduling.
+Products translate their enums and stored payloads into the shared contract. Due eligibility, schedule persistence, retry behavior, and orchestration stay product-specific because model fields, states, snooze behavior, and tenant constraints differ.
## Frontend contract
diff --git a/.agents/skills/adding-product-alerting/references/extending-platform-alerting.md b/.agents/skills/adding-product-alerting/references/extending-platform-alerting.md
index b85349533d8c..7b29a8412eee 100644
--- a/.agents/skills/adding-product-alerting/references/extending-platform-alerting.md
+++ b/.agents/skills/adding-product-alerting/references/extending-platform-alerting.md
@@ -7,7 +7,7 @@ Use this path when adding a reusable alert capability, option, or advanced behav
| Capability | Primary source of truth | Also inspect |
| -------------------------------------------------------------------------------- | -------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| Lifecycle state, notification action, control-plane transition, or policy option | `common/alerting/state_machine.py` | Shared decision tests, every adopter policy and adapter, semgrep rule |
-| Fixed-cadence or shard behavior | `common/alerting/scheduling.py` | Product wrappers, create/update paths, due queries, scheduler interval |
+| Fixed-cadence, calendar, timezone, or schedule-restriction behavior | `products/alerts/backend/scheduling.py` | Product wrappers, create/update paths, due queries, scheduler interval, DST boundaries |
| Destination type or destination-wide option | `products/alerts/backend/destination_configs.py` | HogFunction templates/sub-templates, facade exports, product allowlists, `AlertWizard` |
| HogFunction persistence or delivery semantics | `products/alerts/backend/destinations.py` | Worker batching, rollback, delivery metrics, destination tests |
| Email transport capability | `products/alerts/backend/email_notifications.py` | Facade export, campaign-key semantics, adopter templates and tests |
@@ -29,10 +29,12 @@ If the change crosses rows, update each row deliberately. Do not hide a cross-la
### Scheduling
-- Keep fixed-grid math separate from calendar and timezone rules.
+- Keep scheduling helpers pure Python with no Django or product model imports.
+- Keep fixed-grid and calendar contracts explicit instead of branching on product names.
- Make scheduler interval assumptions explicit.
- Preserve deterministic UUID-based sharding and stable steady-state cadence.
-- Test missed intervals, drift healing, cadence changes, and boundary times.
+- Preserve local wall-clock anchors across DST changes and evaluate restrictions in the team's timezone.
+- Test missed intervals, drift healing, cadence changes, DST transitions, overnight windows, and boundary times.
- Keep due eligibility with the product unless a real cross-product model contract exists.
### Destinations and advanced destination options