: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
feat(signals): schedule anomaly scoring for watched insights (#122227)
posthog[bot]d94142ea8536
diff --git a/.github/new-events-schema-targets.txt b/.github/new-events-schema-targets.txtindex f995fa0285d1..d1ab46afd897 100644--- a/.github/new-events-schema-targets.txt+++ b/.github/new-events-schema-targets.txt@@ -46,6 +46,7 @@ products/mcp_analytics/backend/tests/test_tool_tables.pyproducts/product_analytics/backend/hogql_queriesproducts/product_analytics/backend/tests/test_insight_data_checks.pyproducts/replay_vision/backend/tests/test_session_identity.py+products/signals/backend/test/test_anomaly_scoring_tick.pyproducts/web_analytics/backend/hogql_queries# Code outside HogQL that builds events SQL itself and branches on the schemadiff --git a/posthog/temporal/schedule.py b/posthog/temporal/schedule.pyindex 0da7b6588581..9cde1c577cb6 100644--- a/posthog/temporal/schedule.py+++ b/posthog/temporal/schedule.py@@ -135,6 +135,7 @@ from products.replay_vision.backend.temporal.vision_alerts.schedule import creatfrom products.review_hog.backend.facade.temporal import create_review_hog_finding_outcomes_schedulefrom products.security.backend.facade.temporal import create_sync_access_rules_schedulefrom products.signals.backend.activity_summary.schedule import create_activity_summary_tick_schedule+from products.signals.backend.anomaly_scores.schedule import create_anomaly_scoring_tick_schedulefrom products.signals.backend.emission.conversations_schedule import create_conversations_signals_coordinator_schedulefrom products.signals.backend.ranking.schedule import create_inbox_ranking_scoring_schedulefrom products.signals.backend.temporal.agentic.schedule import (@@ -1000,6 +1001,7 @@ schedules = [create_signals_scout_coordinator_schedule,create_inbox_ranking_scoring_schedule,create_activity_summary_tick_schedule,+ create_anomaly_scoring_tick_schedule,create_scout_suggestions_coordinator_schedule,create_support_reply_coordinator_schedule,create_channel_summary_coordinator_schedule,diff --git a/products/product_analytics/backend/facade/api.py b/products/product_analytics/backend/facade/api.pyindex a04e7c651097..3bcc30aee2c2 100644--- a/products/product_analytics/backend/facade/api.py+++ b/products/product_analytics/backend/facade/api.py@@ -29,6 +29,7 @@ from products.product_analytics.backend.facade.contracts import (InsightSeries,InsightVariableDefinition,SavedInsightDefinition,+ WatchedTrendsInsight,)from products.product_analytics.backend.models.insight import Insightfrom products.product_analytics.backend.models.insight_variable import InsightVariable@@ -131,6 +132,22 @@ def insights_including_soft_deleted_for_team(*, team_id: int, insight_ids: Collereturn logic.insights_including_soft_deleted_for_team(team_id=team_id, insight_ids=insight_ids)+def watched_trends_insights(+ *,+ team_ids: Collection[int],+ intervals: Collection[str],+ excluded_displays: Collection[str],+ viewed_since: datetime,+) -> list[WatchedTrendsInsight]:+ """Saved trends insights that are on a live dashboard or that someone viewed since ``viewed_since``.++ Only insights with an interval in ``intervals`` and a display outside ``excluded_displays`` are kept.+ """+ return logic.watched_trends_insights(+ team_ids=team_ids, intervals=intervals, excluded_displays=excluded_displays, viewed_since=viewed_since+ )++def recent_viewers_by_insight(*, team_id: int, insight_ids: Collection[int], since: datetime, max_per_insight: int) -> dict[int, list["User"]]:diff --git a/products/product_analytics/backend/facade/contracts.py b/products/product_analytics/backend/facade/contracts.pyindex 93029dac081a..fb5f29db504e 100644--- a/products/product_analytics/backend/facade/contracts.py+++ b/products/product_analytics/backend/facade/contracts.py@@ -138,6 +138,14 @@ class InsightDataChecks:coverage: InsightDataChecksCoverage | None+@dataclass(frozen=True)+class WatchedTrendsInsight:+ team_id: int+ insight_id: int+ # The trends interval, "day" when the query sets none.+ interval: str++@dataclass(frozen=True)class TeamEmailAccess:# A match reveals that an email belongs to the team, so each source needs the caller to be allowed to read it.diff --git a/products/product_analytics/backend/logic.py b/products/product_analytics/backend/logic.pyindex 75734efb65a8..c8d87760b176 100644--- a/products/product_analytics/backend/logic.py+++ b/products/product_analytics/backend/logic.py@@ -3,10 +3,12 @@ from datetime import datetimefrom typing import TYPE_CHECKING, Anyfrom uuid import UUID-from django.db.models import Count, OuterRef, QuerySet, Subquery+from django.db.models import Count, OuterRef, Q, QuerySet, Subquery, TextField, Value+from django.db.models.fields.json import KT+from django.db.models.functions import Coalescefrom django.utils.timezone import now-from products.product_analytics.backend.facade.contracts import InsightVariableDefinition+from products.product_analytics.backend.facade.contracts import InsightVariableDefinition, WatchedTrendsInsightfrom products.product_analytics.backend.models.insight import Insight, InsightViewedfrom products.product_analytics.backend.models.insight_variable import InsightVariable@@ -93,6 +95,48 @@ def insights_including_soft_deleted_for_team(*, team_id: int, insight_ids: Collereturn list(Insight.objects_including_soft_deleted.filter(team_id=team_id, id__in=insight_ids))+def watched_trends_insights(+ *,+ team_ids: Collection[int],+ intervals: Collection[str],+ excluded_displays: Collection[str],+ viewed_since: datetime,+) -> list[WatchedTrendsInsight]:+ if not team_ids:+ return []+ trends = (+ Insight.objects.filter(team_id__in=team_ids, saved=True)+ .annotate(+ # A wrapper node holds the trends query in `source`.+ trends_kind=Coalesce(KT("query__source__kind"), KT("query__kind")),+ trends_interval=Coalesce(+ KT("query__source__interval"), KT("query__interval"), Value("day"), output_field=TextField()+ ),+ trends_display=Coalesce(+ KT("query__source__trendsFilter__display"),+ KT("query__trendsFilter__display"),+ Value(""),+ output_field=TextField(),+ ),+ )+ .filter(trends_kind="TrendsQuery", trends_interval__in=intervals)+ .exclude(trends_display__in=excluded_displays)+ )+ # Two queries instead of one OR, so Postgres never multiplies dashboard tiles by views.+ on_dashboard = trends.filter(+ Q(dashboard_tiles__deleted=False) | Q(dashboard_tiles__deleted__isnull=True),+ dashboard_tiles__dashboard__deleted=False,+ )+ viewed = trends.filter(insightviewed__last_viewed_at__gte=viewed_since)+ rows: set[tuple[int, int, str]] = set()+ for queryset in (on_dashboard, viewed):+ rows.update(queryset.values_list("team_id", "id", "trends_interval"))+ return [+ WatchedTrendsInsight(team_id=team_id, insight_id=insight_id, interval=interval)+ for team_id, insight_id, interval in sorted(rows)+ ]++def recent_viewers_by_insight(*, team_id: int, insight_ids: Collection[int], since: datetime, max_per_insight: int) -> dict[int, list["User"]]:diff --git a/products/signals/backend/anomaly_scores/config.py b/products/signals/backend/anomaly_scores/config.pyindex 1ccfb1511f7b..14ea7ff644eb 100644--- a/products/signals/backend/anomaly_scores/config.py+++ b/products/signals/backend/anomaly_scores/config.py@@ -1,14 +1,32 @@+"""Scoring settings, and the levers of the scheduled tick, read from the `anomaly-scoring` flag payload.++The payload parse fails closed: a missing, malformed or unreadable payload turns the tick off. An invalid+field falls back to its default, and no default is unbounded.+"""++import uuid+import dataclassesfrom typing import TYPE_CHECKING, Any+import structlog+from posthog.schema import IntervalTypefrom posthog.dataclasses import frozen+from products.signals.backend.scout_harness.team_limits import read_flag_payload+if TYPE_CHECKING:from posthog.models import Teamfrom products.product_analytics.backend.facade.models import Insight+logger = structlog.get_logger(__name__)++ANOMALY_SCORING_FLAG = "anomaly-scoring"+# Not a person: the flag must serve its payload to this id.+ANOMALY_SCORING_FLAG_DISTINCT_ID = "signals_anomaly_scoring"+@frozenclass IntervalScoring:@@ -54,8 +72,112 @@ DEFAULT_PROFILE = ScoringProfile()DEFAULT_LIMITS = ScoringLimits()+# The largest value a flag payload can set for each limit.+MAX_LIMITS = ScoringLimits(max_breakdown_values=25, max_series=50, max_backfill_buckets=90)def resolve_profile(team: "Team", insight: "Insight") -> ScoringProfile:"""The one place that picks scoring settings for an insight, so project and per-metric overrides only change this."""return DEFAULT_PROFILE+++# Hourly insights are left out by default: with many eligible insights, the time between two visits of+# one insight passes the hourly backfill cap, and the buckets in between are never scored.+DEFAULT_TICK_INTERVALS = (IntervalType.DAY, IntervalType.WEEK, IntervalType.MONTH)+++@frozen+class AnomalyScoringConfig:+ enabled: bool = False+ organization_ids: tuple[str, ...] = ()+ # Empty means every team of the organizations in `organization_ids`.+ allow_team_ids: tuple[int, ...] = ()+ skip_team_ids: tuple[int, ...] = ()+ intervals: tuple[IntervalType, ...] = DEFAULT_TICK_INTERVALS+ # The budget is scoring queries, not events. One insight is one query.+ max_insights_per_tick: int = 8+ max_concurrency: int = 2+ limits: ScoringLimits = DEFAULT_LIMITS+++DISABLED = AnomalyScoringConfig()+++def _positive_int(value: Any, default: int) -> int:+ # `bool` is an `int` subclass, and `true` is not a count.+ return value if isinstance(value, int) and not isinstance(value, bool) and value > 0 else default+++def _team_ids(value: Any) -> tuple[int, ...]:+ if not isinstance(value, list):+ return ()+ return tuple(item for item in value if isinstance(item, int) and not isinstance(item, bool))+++def _is_uuid(value: str) -> bool:+ try:+ uuid.UUID(value)+ except ValueError:+ return False+ return True+++def _organization_ids(value: Any) -> tuple[str, ...]:+ if not isinstance(value, list):+ return ()+ # A value that is not a UUID would fail the organization lookup of the whole tick.+ return tuple(item for item in value if isinstance(item, str) and _is_uuid(item))+++def _intervals(value: Any) -> tuple[IntervalType, ...]:+ if not isinstance(value, list):+ return DEFAULT_TICK_INTERVALS+ scored = {interval.value: interval for interval in DEFAULT_PROFILE.intervals}+ intervals = tuple(dict.fromkeys(scored[item] for item in value if isinstance(item, str) and item in scored))+ return intervals or DEFAULT_TICK_INTERVALS+++def parse_limits(payload: Any) -> ScoringLimits:+ """The `limits` object of the payload. An invalid field gets its default, a large one its `MAX_LIMITS` value."""+ if not isinstance(payload, dict):+ return DEFAULT_LIMITS+ return ScoringLimits(+ **{+ field.name: min(+ _positive_int(payload.get(field.name), getattr(DEFAULT_LIMITS, field.name)),+ getattr(MAX_LIMITS, field.name),+ )+ for field in dataclasses.fields(ScoringLimits)+ }+ )+++def parse_config(payload: Any) -> AnomalyScoringConfig:+ if not isinstance(payload, dict) or payload.get("enabled") is not True:+ return DISABLED+ defaults = AnomalyScoringConfig()+ return AnomalyScoringConfig(+ enabled=True,+ organization_ids=_organization_ids(payload.get("organization_ids")),+ allow_team_ids=_team_ids(payload.get("allow_team_ids")),+ skip_team_ids=_team_ids(payload.get("skip_team_ids")),+ intervals=_intervals(payload.get("intervals")),+ max_insights_per_tick=_positive_int(payload.get("max_insights_per_tick"), defaults.max_insights_per_tick),+ max_concurrency=_positive_int(payload.get("max_concurrency"), defaults.max_concurrency),+ limits=parse_limits(payload.get("limits")),+ )+++def read_payload() -> dict | None:+ return read_flag_payload(ANOMALY_SCORING_FLAG, ANOMALY_SCORING_FLAG_DISTINCT_ID)+++def read_config(payload: Any) -> AnomalyScoringConfig:+ config = parse_config(payload)+ logger.info(+ "anomaly_scoring_config_read",+ enabled=config.enabled,+ organizations=len(config.organization_ids),+ allow_teams=len(config.allow_team_ids),+ )+ return configdiff --git a/products/signals/backend/anomaly_scores/emission.py b/products/signals/backend/anomaly_scores/emission.pynew file mode 100644index 000000000000..bab05e30abe5--- /dev/null+++ b/products/signals/backend/anomaly_scores/emission.py@@ -0,0 +1,97 @@+"""Turn scored buckets into `$anomaly_score` events in the project's own events."""++import json+import uuid+import datetime+from typing import Any++from posthog.api.capture import capture_batch_internal+from posthog.models import Team++from products.signals.backend.anomaly_scores.queue import SCORE_EVENT, SOURCE_INSIGHT+from products.signals.backend.anomaly_scores.scoring import InsightScores, ScoredPoint, ScoredSeries++EVENT_SOURCE = "signals_anomaly_scores"+# Not a person. Every score event of every project uses it, and no person profile is made for it.+SCORER_DISTINCT_ID = "$posthog_anomaly_scorer"+# A deterministic uuid per series, bucket and scoring version, so a resent bucket replaces its earlier row.+_EVENT_UUID_NAMESPACE = uuid.UUID("6a0f4b9e-2c1d-4e57-9b3a-8d2e1f7c5a40")+++def _event(+ *,+ team_id: int,+ insight_id: int,+ scores: InsightScores,+ series: ScoredSeries,+ point: ScoredPoint,+ scored_at: datetime.datetime,+) -> dict[str, Any]:+ bucket_start_utc = point.bucket_start.astimezone(datetime.UTC)+ return {+ "event": SCORE_EVENT,+ "distinct_id": SCORER_DISTINCT_ID,+ # The bucket start, so a trends insight on these events lines up with the source chart.+ "timestamp": bucket_start_utc.isoformat(),+ "event_uuid": str(+ uuid.uuid5(+ _EVENT_UUID_NAMESPACE,+ f"{team_id}:{series.series_key}:{scores.detector_version}:{bucket_start_utc.isoformat()}",+ )+ ),+ "options": {"process_person_profile": False},+ "properties": {+ "$geoip_disable": True,+ "$anomaly_score": point.score,+ "$anomaly_flag": None if point.flag is None else int(point.flag),+ "$anomaly_value": point.value,+ "$anomaly_bucket_start": point.bucket_start.isoformat(),+ "$anomaly_interval": scores.interval.value,+ "$anomaly_series_key": series.series_key,+ "$anomaly_source": SOURCE_INSIGHT,+ "$anomaly_insight_id": insight_id,+ "$anomaly_series_label": series.label,+ "$anomaly_breakdown_value": series.breakdown_value,+ "$anomaly_detector": scores.detector,+ "$anomaly_detector_version": scores.detector_version,+ "$anomaly_profile": scores.profile,+ "$anomaly_scored_at": scored_at.astimezone(datetime.UTC).isoformat(),+ "$anomaly_details": json.dumps(point.details, sort_keys=True),+ },+ }+++def score_events(+ *,+ team_id: int,+ insight_id: int,+ scores: InsightScores,+ scored_through: dict[str, datetime.datetime],+ scored_at: datetime.datetime,+) -> list[dict[str, Any]]:+ """One event per series and bucket that is later than the series' newest scored bucket."""+ events: list[dict[str, Any]] = []+ 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,+ )+ )+ return events+++def emit(team: Team, events: list[dict[str, Any]]) -> None:+ if not events:+ return+ capture_batch_internal(+ events=events, token=team.api_token, event_source=EVENT_SOURCE, process_person_profile=False+ ).raise_for_status()diff --git a/products/signals/backend/anomaly_scores/queue.py b/products/signals/backend/anomaly_scores/queue.pynew file mode 100644index 000000000000..fdbdd41a9ed3--- /dev/null+++ b/products/signals/backend/anomaly_scores/queue.py@@ -0,0 +1,249 @@+"""Pick the watched insights that the next anomaly scoring tick scores.++There is no stored state. One query per team reads the newest scored bucket of each series back+from the `$anomaly_score` events. `selection.pick_order` then orders the due insights.+"""++import datetime+from collections.abc import Collection+from typing import Any, cast++from django.core.cache import cache++from posthog.schema import ChartDisplayType, IntervalType++from posthog.hogql import ast+from posthog.hogql.query import execute_hogql_query++from posthog.clickhouse.client.connection import ClickHouseUser+from posthog.clickhouse.query_tagging import Feature, Product, tag_queries+from posthog.clickhouse.workload import Workload+from posthog.dataclasses import frozen+from posthog.interval_specs import interval_spec+from posthog.models import Team+from posthog.tasks.alerts.utils import NON_TIME_SERIES_DISPLAY_TYPES++from products.alerts_platform.backend.facade.scheduling import CalendarInterval, alert_check_offset+from products.product_analytics.backend.facade.api import watched_trends_insights+from products.signals.backend.anomaly_scores.config import AnomalyScoringConfig+from products.signals.backend.anomaly_scores.scoring import bucket_end+from products.signals.backend.anomaly_scores.selection import (+ Candidate,+ InsightUsage,+ SelectionConfig,+ insight_usage,+ pick_order,+)++from ee.billing.quota_limiting import QuotaLimitingCaches, QuotaResource, list_limited_team_attributes++SCORE_EVENT = "$anomaly_score"+SOURCE_INSIGHT = "insight"+# An insight counts as watched when it is on a dashboard or someone viewed it in this window.+VIEW_WINDOW = datetime.timedelta(days=30)+# One row is one series. Far more than the series of every watched insight of one team.+_MAX_SCORED_SERIES = 50_000+# An insight is not picked again in this time after an attempt. Events take a moment to reach+# ClickHouse, and an insight that writes nothing would otherwise stay at the head of the queue.+ATTEMPT_COOLDOWN = datetime.timedelta(hours=1)+UNSUPPORTED_COOLDOWN = datetime.timedelta(days=1)++_CALENDAR_INTERVALS = {+ IntervalType.HOUR: CalendarInterval.HOURLY,+ IntervalType.DAY: CalendarInterval.DAILY,+ IntervalType.WEEK: CalendarInterval.WEEKLY,+ IntervalType.MONTH: CalendarInterval.MONTHLY,+}+# The scorer rejects these, so selection leaves them out instead of picking them on every tick.+_UNSCORED_DISPLAYS = sorted(+ display.value for display in {*NON_TIME_SERIES_DISPLAY_TYPES, ChartDisplayType.ACTIONS_LINE_GRAPH_CUMULATIVE}+)++_SCORED_BUCKETS_QUERY = """+SELECT+ toInt(properties.$anomaly_insight_id) AS insight_id,+ toString(properties.$anomaly_series_key) AS series_key,+ max(timestamp) AS newest_bucket+FROM events+WHERE event = {event}+ AND timestamp >= {since}+ AND timestamp <= {now}+ AND properties.$anomaly_source = {source}+ AND toInt(properties.$anomaly_insight_id) IN {insight_ids}+GROUP BY insight_id, series_key+LIMIT {limit}+"""+++@frozen+class WatchedInsight:+ team_id: int+ insight_id: int+ interval: IntervalType+++@frozen+class InsightPick:+ team_id: int+ insight_id: int+ # The newest scored bucket start of each series, in UTC. Empty when the insight was never scored.+ scored_through: dict[str, datetime.datetime]+++@frozen+class Selection:+ picks: list[InsightPick]+ # Due insights, picked or not. More than the picks means the tick cap cut the queue.+ candidates: int+++def eligible_teams(config: AnomalyScoringConfig) -> list[Team]:+ if not config.enabled or not config.organization_ids:+ return []+ teams = Team.objects.filter(organization_id__in=config.organization_ids).exclude(id__in=config.skip_team_ids)+ if config.allow_team_ids:+ teams = teams.filter(id__in=config.allow_team_ids)+ # Capture drops every event of a project over its event quota, so scoring it wastes the query.+ limited = set(list_limited_team_attributes(QuotaResource.EVENTS, QuotaLimitingCaches.QUOTA_LIMITER_CACHE_KEY))+ return [team for team in teams if team.api_token not in limited]+++def watched_insights(+ team_ids: list[int], intervals: tuple[IntervalType, ...], now: datetime.datetime+) -> list[WatchedInsight]:+ """Saved trends insights with a scored interval that are on a dashboard or were viewed lately."""+ return [+ WatchedInsight(team_id=watched.team_id, insight_id=watched.insight_id, interval=IntervalType(watched.interval))+ for watched in watched_trends_insights(+ team_ids=team_ids,+ intervals=[interval.value for interval in intervals],+ excluded_displays=_UNSCORED_DISPLAYS,+ viewed_since=now - VIEW_WINDOW,+ )+ ]+++def scored_state_since(config: AnomalyScoringConfig, now: datetime.datetime) -> datetime.datetime:+ """How far back the scored state reaches: one bucket more than the backfill cap of the longest interval."""+ return min(+ now - interval_spec(interval).period * (config.limits.max_backfill_buckets + 1) for interval in config.intervals+ )+++def _utc(value: datetime.datetime) -> datetime.datetime:+ return value.replace(tzinfo=datetime.UTC) if value.tzinfo is None else value.astimezone(datetime.UTC)+++def scored_buckets(+ team: Team, insight_ids: Collection[int], since: datetime.datetime, now: datetime.datetime+) -> dict[int, dict[str, datetime.datetime]]:+ """Insight id to the newest scored bucket start of each of its series, for this team's own insights.++ Any member can capture an event with any properties into the project. Rows for another team's+ insight, or with a future bucket, are ignored, so they cannot stop the scoring of an insight.+ """+ if not insight_ids:+ return {}+ tag_queries(product=Product.SIGNALS, feature=Feature.ALERTING)+ response = execute_hogql_query(+ _SCORED_BUCKETS_QUERY,+ team,+ query_type="AnomalyScoresScoredBuckets",+ placeholders={+ "event": ast.Constant(value=SCORE_EVENT),+ "source": ast.Constant(value=SOURCE_INSIGHT),+ "since": ast.Constant(value=since),+ "now": ast.Constant(value=now),+ "insight_ids": ast.Tuple(exprs=[ast.Constant(value=insight_id) for insight_id in sorted(insight_ids)]),+ "limit": ast.Constant(value=_MAX_SCORED_SERIES),+ },+ workload=Workload.OFFLINE,+ ch_user=ClickHouseUser.SIGNALS,+ )+ state: dict[int, dict[str, datetime.datetime]] = {}+ for insight_id, series_key, newest_bucket in cast(list[tuple[Any, ...]], response.results or []):+ if insight_id is None or not series_key:+ continue+ state.setdefault(int(insight_id), {})[str(series_key)] = _utc(newest_bucket)+ return state+++def is_due(+ insight: WatchedInsight, newest_bucket: datetime.datetime | None, timezone: datetime.tzinfo, now: datetime.datetime+) -> bool:+ """The bucket after the newest scored one is complete, and this insight's slot after its end has passed."""+ if newest_bucket is None:+ return True+ next_start = bucket_end(newest_bucket.astimezone(timezone), insight.interval)+ next_end = bucket_end(next_start, insight.interval)+ # The same offset as alert checks, so insights spread over the window after each boundary.+ offset = alert_check_offset(_CALENDAR_INTERVALS[insight.interval], f"anomaly:{insight.insight_id}")+ return now >= next_end + offset+++def _cooldown_key(insight_id: int) -> str:+ return f"signals:anomaly_scores:attempted:{insight_id}"+++def mark_attempted(insight_id: int, cooldown: datetime.timedelta) -> None:+ cache.set(_cooldown_key(insight_id), True, timeout=int(cooldown.total_seconds()))+++def _cooling_down(insight_ids: list[int]) -> set[int]:+ if not insight_ids:+ return set()+ found = cache.get_many([_cooldown_key(insight_id) for insight_id in insight_ids])+ return {insight_id for insight_id in insight_ids if _cooldown_key(insight_id) in found}+++def select_insights(+ config: AnomalyScoringConfig, selection_config: SelectionConfig, now: datetime.datetime, limit: int+) -> Selection:+ teams = {team.id: team for team in eligible_teams(config)}+ insights = watched_insights(list(teams), config.intervals, now)+ since = scored_state_since(config, now)+ state: dict[int, dict[str, datetime.datetime]] = {}+ for team_id in sorted({insight.team_id for insight in insights}):+ team_insight_ids = {insight.insight_id for insight in insights if insight.team_id == team_id}+ state.update(scored_buckets(teams[team_id], team_insight_ids, since, now))++ newest_buckets = {+ insight.insight_id: max(state[insight.insight_id].values()) if insight.insight_id in state else None+ for insight in insights+ }+ due = [+ insight+ for insight in insights+ if is_due(insight, newest_buckets[insight.insight_id], teams[insight.team_id].timezone_info, now)+ ]+ cooling = _cooling_down([insight.insight_id for insight in due])+ due_by_id = {insight.insight_id: insight for insight in due if insight.insight_id not in cooling}++ usage: dict[int, InsightUsage] = {}+ for team_id in sorted({insight.team_id for insight in due_by_id.values()}):+ due_ids = [insight.insight_id for insight in due_by_id.values() if insight.team_id == team_id]+ usage.update(insight_usage(team_id=team_id, insight_ids=due_ids, now=now))+ candidates = []+ for insight in due_by_id.values():+ newest = newest_buckets[insight.insight_id]+ candidates.append(+ Candidate(+ insight_id=insight.insight_id,+ interval=insight.interval,+ # Staleness steps whole buckets, so it needs the bucket start in the project timezone.+ newest_scored_bucket=None+ if newest is None+ else newest.astimezone(teams[insight.team_id].timezone_info),+ usage=usage[insight.insight_id],+ )+ )+ order = pick_order(+ candidates, config=selection_config, tick_start=now, max_staleness=config.limits.max_backfill_buckets+ )+ picks = [+ InsightPick(+ team_id=due_by_id[insight_id].team_id, insight_id=insight_id, scored_through=state.get(insight_id, {})+ )+ for insight_id in order[:limit]+ ]+ return Selection(picks=picks, candidates=len(order))diff --git a/products/signals/backend/anomaly_scores/schedule.py b/products/signals/backend/anomaly_scores/schedule.pynew file mode 100644index 000000000000..49365286ba56--- /dev/null+++ b/products/signals/backend/anomaly_scores/schedule.py@@ -0,0 +1,58 @@+"""Schedule registration for the anomaly scoring tick."""++from dataclasses import asdict+from datetime import timedelta++from django.conf import settings++from temporalio.client import (+ Client,+ Schedule,+ ScheduleActionStartWorkflow,+ ScheduleIntervalSpec,+ ScheduleOverlapPolicy,+ SchedulePolicy,+ ScheduleSpec,+)++from posthog.scheduling.jitter import deterministic_offset+from posthog.temporal.common.schedule import a_create_schedule, a_schedule_exists, a_update_schedule++from products.signals.backend.anomaly_scores.tick import TICK_WORKFLOW_NAME, AnomalyScoringTickInput++ANOMALY_SCORING_TICK_SCHEDULE_ID = "anomaly-scoring-tick-schedule"+ANOMALY_SCORING_TICK_INTERVAL = timedelta(minutes=5)+++async def create_anomaly_scoring_tick_schedule(client: Client) -> None:+ """Create or update the tick schedule on the self-driving task queue.++ The schedule exists in every environment. The `anomaly-scoring` flag gates the work, so turning+ the tick on or off needs no schedule change. SKIP on overlap: two ticks must not score the same+ insight at the same time, and a slow tick leaves the rest of the queue to the next one.+ """+ schedule = Schedule(+ action=ScheduleActionStartWorkflow(+ TICK_WORKFLOW_NAME,+ asdict(AnomalyScoringTickInput()),+ id=ANOMALY_SCORING_TICK_SCHEDULE_ID,+ task_queue=settings.SELF_DRIVING_TASK_QUEUE,+ execution_timeout=ANOMALY_SCORING_TICK_INTERVAL,+ ),+ spec=ScheduleSpec(+ intervals=[+ ScheduleIntervalSpec(+ every=ANOMALY_SCORING_TICK_INTERVAL,+ offset=deterministic_offset(ANOMALY_SCORING_TICK_SCHEDULE_ID, ANOMALY_SCORING_TICK_INTERVAL),+ )+ ]+ ),+ policy=SchedulePolicy(overlap=ScheduleOverlapPolicy.SKIP),+ )+ if await a_schedule_exists(client, ANOMALY_SCORING_TICK_SCHEDULE_ID):+ # Keep the live state, so a deploy does not resume a schedule that an operator paused.+ description = await client.get_schedule_handle(ANOMALY_SCORING_TICK_SCHEDULE_ID).describe()+ schedule.state = description.schedule.state+ await a_update_schedule(client, ANOMALY_SCORING_TICK_SCHEDULE_ID, schedule)+ else:+ await a_create_schedule(client, ANOMALY_SCORING_TICK_SCHEDULE_ID, schedule, trigger_immediately=False)diff --git a/products/signals/backend/anomaly_scores/scoring.py b/products/signals/backend/anomaly_scores/scoring.pyindex 2c6b65f5bfe7..c1dbf75ff556 100644--- a/products/signals/backend/anomaly_scores/scoring.py+++ b/products/signals/backend/anomaly_scores/scoring.py@@ -10,6 +10,8 @@ import numpy as npfrom posthog.schema import BreakdownFilter, ChartDisplayType, DateRange, IntervalType, TrendsQuery+from posthog.hogql.constants import LimitContext+from posthog.api.services.query import ExecutionModefrom posthog.caching.calculate_results import calculate_for_query_based_insightfrom posthog.clickhouse.query_tagging import Feature, Product, tag_queries@@ -193,6 +195,8 @@ def score_insight(user=insight.created_by,query_override=scoring_query.model_dump(exclude_none=True),analytics_props={"source": EventSource.SELF_DRIVING},+ # The async context raises the ClickHouse timeout to minutes. One slow insight must not hold a tick.+ limit_context=LimitContext.QUERY,)if calculation.result is None:raise RuntimeError(f"No results for insight {insight.pk}")diff --git a/products/signals/backend/anomaly_scores/tick.py b/products/signals/backend/anomaly_scores/tick.pynew file mode 100644index 000000000000..67f5dc219f96--- /dev/null+++ b/products/signals/backend/anomaly_scores/tick.py@@ -0,0 +1,247 @@+"""The scheduled anomaly scoring tick: a Temporal workflow that scores the due watched insights.++The `anomaly-scoring` flag payload holds every lever, and a missing or malformed payload turns the+tick off. Each insight activity scores one insight and writes its new buckets as `$anomaly_score` events.+"""++import asyncio+import datetime+from collections import Counter++from django.conf import settings+from django.utils import timezone++import structlog+from temporalio import activity, workflow+from temporalio.common import RetryPolicy++from posthog.dataclasses import frozen+from posthog.models import Team+from posthog.ph_client import ph_background_capture+from posthog.sync import database_sync_to_async+from posthog.temporal.common.heartbeat import Heartbeater+from posthog.temporal.common.scoped import scoped_temporal+from posthog.temporal.common.utils import close_db_connections++from products.product_analytics.backend.facade.api import insights_including_soft_deleted_for_team+from products.signals.backend.anomaly_scores.config import (+ ANOMALY_SCORING_FLAG_DISTINCT_ID,+ DEFAULT_LIMITS,+ ScoringLimits,+ read_config,+ read_payload,+)+from products.signals.backend.anomaly_scores.emission import emit, score_events+from products.signals.backend.anomaly_scores.queue import (+ ATTEMPT_COOLDOWN,+ UNSUPPORTED_COOLDOWN,+ InsightPick,+ mark_attempted,+ select_insights,+)+from products.signals.backend.anomaly_scores.scoring import UnsupportedInsightError, score_insight+from products.signals.backend.anomaly_scores.selection import parse_selection_config++logger = structlog.get_logger(__name__)++TICK_WORKFLOW_NAME = "anomaly-scoring-tick"++STATUS_SCORED = "scored"+# Scored, with no completed bucket newer than the ones already written.+STATUS_UP_TO_DATE = "up_to_date"+STATUS_UNSUPPORTED = "unsupported"+STATUS_MISSING = "missing"++_PLAN_TIMEOUT = datetime.timedelta(minutes=2)+# The scoring query times out at 60 seconds. The rest is detection and the capture request.+_INSIGHT_TIMEOUT = datetime.timedelta(minutes=3)+_INSIGHT_HEARTBEAT_TIMEOUT = datetime.timedelta(minutes=1)+++@frozen+class AnomalyScoringTickInput:+ # A manual run can override the per-tick cap of the flag.+ limit: int | None = None+++@frozen+class AnomalyScoringTickPlan:+ enabled: bool+ picks: list[InsightPick]+ candidates: int+ max_concurrency: int+ limits: ScoringLimits = DEFAULT_LIMITS+++@frozen+class ScoreInsightInput:+ pick: InsightPick+ limits: ScoringLimits = DEFAULT_LIMITS+++@frozen+class ScoreInsightResult:+ status: str+ events: int+++@frozen+class AnomalyScoringTickResult:+ enabled: bool = False+ candidates: int = 0+ picked: int = 0+ statuses: dict[str, int] | None = None+ errored: int = 0+ events: int = 0+++def plan_tick(limit: int | None, now: datetime.datetime) -> AnomalyScoringTickPlan:+ # One read, so the scoring and selection levers come from the same payload.+ payload = read_payload()+ config = read_config(payload)+ if not config.enabled:+ return AnomalyScoringTickPlan(enabled=False, picks=[], candidates=0, max_concurrency=config.max_concurrency)+ selection = select_insights(config, parse_selection_config(payload), now, limit or config.max_insights_per_tick)+ return AnomalyScoringTickPlan(+ enabled=True,+ picks=selection.picks,+ candidates=selection.candidates,+ max_concurrency=config.max_concurrency,+ limits=config.limits,+ )+++def run_insight(input: ScoreInsightInput, now: datetime.datetime) -> ScoreInsightResult:+ pick = input.pick+ insight = next(+ (+ insight+ for insight in insights_including_soft_deleted_for_team(team_id=pick.team_id, insight_ids=[pick.insight_id])+ if insight.saved and not insight.deleted+ ),+ None,+ )+ if insight is None:+ return ScoreInsightResult(status=STATUS_MISSING, events=0)+ team = Team.objects.get(id=pick.team_id)+ # Set before scoring, so a failure also waits for the cooldown instead of taking a slot on every tick.+ mark_attempted(pick.insight_id, ATTEMPT_COOLDOWN)+ try:+ scores = score_insight(+ insight,+ team,+ now=now,+ # Every series is filtered again on its own newest bucket below. A new series gets up to the backfill cap.+ after=min(pick.scored_through.values()) if pick.scored_through else None,+ limits=input.limits,+ )+ except UnsupportedInsightError as error:+ logger.info("anomaly_scoring_insight_unsupported", insight_id=pick.insight_id, reason=str(error))+ mark_attempted(pick.insight_id, UNSUPPORTED_COOLDOWN)+ return ScoreInsightResult(status=STATUS_UNSUPPORTED, events=0)+ events = score_events(+ team_id=team.id, insight_id=insight.id, scores=scores, scored_through=pick.scored_through, scored_at=now+ )+ emit(team, events)+ return ScoreInsightResult(status=STATUS_SCORED if events else STATUS_UP_TO_DATE, events=len(events))+++def capture_tick(result: AnomalyScoringTickResult) -> None:+ logger.info(+ "anomaly_scoring_tick_finished",+ candidates=result.candidates,+ picked=result.picked,+ statuses=result.statuses,+ errored=result.errored,+ events=result.events,+ )+ ph_background_capture()(+ distinct_id=ANOMALY_SCORING_FLAG_DISTINCT_ID,+ event="anomaly scoring tick",+ properties={+ "deployment": settings.CLOUD_DEPLOYMENT,+ "candidates": result.candidates,+ "picked": result.picked,+ **{f"status_{status}": count for status, count in (result.statuses or {}).items()},+ "errored": result.errored,+ "events": result.events,+ },+ )+++@scoped_temporal()+@close_db_connections+async def plan_anomaly_scoring_tick_activity(input: AnomalyScoringTickInput) -> AnomalyScoringTickPlan:+ return await database_sync_to_async(plan_tick, thread_sensitive=False)(input.limit, timezone.now())+++@scoped_temporal()+@close_db_connections+async def score_anomaly_insight_activity(input: ScoreInsightInput) -> ScoreInsightResult:+ async with Heartbeater():+ return await database_sync_to_async(run_insight, thread_sensitive=False)(input, timezone.now())+++@scoped_temporal()+@close_db_connections+async def capture_anomaly_scoring_tick_activity(result: AnomalyScoringTickResult) -> None:+ await database_sync_to_async(capture_tick, thread_sensitive=False)(result)++[email protected](name=TICK_WORKFLOW_NAME)+class AnomalyScoringTickWorkflow:+ @workflow.run+ async def run(self, inputs: AnomalyScoringTickInput) -> AnomalyScoringTickResult:+ plan = await workflow.execute_activity(+ plan_anomaly_scoring_tick_activity,+ inputs,+ start_to_close_timeout=_PLAN_TIMEOUT,+ retry_policy=RetryPolicy(maximum_attempts=2),+ )+ if not plan.enabled:+ return AnomalyScoringTickResult()++ statuses: Counter[str] = Counter()+ errored = 0+ events = 0+ concurrency = max(1, plan.max_concurrency)+ for index in range(0, len(plan.picks), concurrency):+ results = await asyncio.gather(+ *(+ workflow.execute_activity(+ score_anomaly_insight_activity,+ ScoreInsightInput(pick=pick, limits=plan.limits),+ start_to_close_timeout=_INSIGHT_TIMEOUT,+ heartbeat_timeout=_INSIGHT_HEARTBEAT_TIMEOUT,+ # A failed insight waits for its cooldown and a later tick, not a retry in this one.+ retry_policy=RetryPolicy(maximum_attempts=1),+ )+ for pick in plan.picks[index : index + concurrency]+ ),+ return_exceptions=True,+ )+ for outcome in results:+ if isinstance(outcome, BaseException):+ errored += 1+ continue+ statuses[outcome.status] += 1+ events += outcome.events++ result = AnomalyScoringTickResult(+ enabled=True,+ candidates=plan.candidates,+ picked=len(plan.picks),+ statuses=dict(statuses),+ errored=errored,+ events=events,+ )+ await workflow.execute_activity(+ capture_anomaly_scoring_tick_activity,+ result,+ start_to_close_timeout=datetime.timedelta(minutes=1),+ retry_policy=RetryPolicy(maximum_attempts=2),+ )+ return resultdiff --git a/products/signals/backend/temporal/__init__.py b/products/signals/backend/temporal/__init__.pyindex 1d4435c42eca..b0950c52fb9b 100644--- a/products/signals/backend/temporal/__init__.py+++ b/products/signals/backend/temporal/__init__.py@@ -4,6 +4,12 @@ from products.signals.backend.activity_summary.tick import (plan_activity_summary_tick_activity,update_activity_summary_pair_activity,)+from products.signals.backend.anomaly_scores.tick import (+ AnomalyScoringTickWorkflow,+ capture_anomaly_scoring_tick_activity,+ plan_anomaly_scoring_tick_activity,+ score_anomaly_insight_activity,+)from products.signals.backend.ranking.sweep import InboxRankingScoringWorkflow, score_inbox_reports_activityfrom products.signals.backend.temporal.agentic.report import run_agentic_report_activityfrom products.signals.backend.temporal.agentic.scout_coordinator import (@@ -224,10 +230,13 @@ ACTIVITIES = []# The temporal-worker-self-driving fleet runs these, not video-export.-SELF_DRIVING_WORKFLOWS = [InboxRankingScoringWorkflow, ActivitySummaryTickWorkflow]+SELF_DRIVING_WORKFLOWS = [InboxRankingScoringWorkflow, ActivitySummaryTickWorkflow, AnomalyScoringTickWorkflow]SELF_DRIVING_ACTIVITIES = [score_inbox_reports_activity,plan_activity_summary_tick_activity,update_activity_summary_pair_activity,capture_activity_summary_tick_activity,+ plan_anomaly_scoring_tick_activity,+ score_anomaly_insight_activity,+ capture_anomaly_scoring_tick_activity,]diff --git a/products/signals/backend/test/test_anomaly_scoring_tick.py b/products/signals/backend/test/test_anomaly_scoring_tick.pynew file mode 100644index 000000000000..a6aaa3317e0e--- /dev/null+++ b/products/signals/backend/test/test_anomaly_scoring_tick.py@@ -0,0 +1,341 @@+from datetime import UTC, datetime, timedelta+from typing import Any+from zoneinfo import ZoneInfo++import time_machine+from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event+from unittest.mock import patch++from django.core.cache import cache+from django.test import SimpleTestCase++from parameterized import parameterized++from posthog.schema import IntervalType++from posthog.models import Organization, Team++from products.dashboards.backend.models.dashboard import Dashboard+from products.dashboards.backend.models.dashboard_tile import DashboardTile+from products.product_analytics.backend.facade.api import record_insight_view+from products.product_analytics.backend.facade.models import Insight+from products.signals.backend.anomaly_scores.config import DISABLED, AnomalyScoringConfig, parse_config+from products.signals.backend.anomaly_scores.emission import SCORER_DISTINCT_ID, score_events+from products.signals.backend.anomaly_scores.queue import (+ WatchedInsight,+ _cooldown_key,+ eligible_teams,+ is_due,+ scored_buckets,+ select_insights,+ watched_insights,+)+from products.signals.backend.anomaly_scores.scoring import InsightScores, ScoredPoint, ScoredSeries+from products.signals.backend.anomaly_scores.selection import SelectionConfig, Weighting+from products.signals.backend.anomaly_scores.tick import plan_tick++NEW_YORK = ZoneInfo("America/New_York")+ORGANIZATION_ID = "4dc8564d-bd82-1065-2f40-97f7c50f67cf"+NOW = datetime(2026, 10, 22, 12, tzinfo=UTC)+++def _trends(**source: Any) -> dict[str, Any]:+ return {+ "kind": "InsightVizNode",+ "source": {"kind": "TrendsQuery", "series": [{"kind": "EventsNode", "event": "$pageview"}], **source},+ }+++def _scores(*series: ScoredSeries) -> InsightScores:+ return InsightScores(+ interval=IntervalType.DAY, profile="default-v1", detector="zscore", detector_version="v1", series=list(series)+ )+++def _series(key: str, starts: list[datetime]) -> ScoredSeries:+ return ScoredSeries(+ series_key=key,+ label=key,+ breakdown_value=None,+ points=[ScoredPoint(bucket_start=start, value=1.0, score=0.5, flag=False, details={}) for start in starts],+ )+++class TestTickConfig(SimpleTestCase):+ @parameterized.expand(+ [+ ("missing", None),+ ("not_an_object", "enabled"),+ ("no_enabled", {"organization_ids": [ORGANIZATION_ID]}),+ ("enabled_not_bool", {"enabled": "true", "organization_ids": [ORGANIZATION_ID]}),+ ("disabled", {"enabled": False, "organization_ids": [ORGANIZATION_ID], "allow_team_ids": [2]}),+ ]+ )+ def test_a_missing_or_malformed_payload_does_no_work(self, _name: str, payload: Any) -> None:+ with (+ patch("products.signals.backend.anomaly_scores.config.read_flag_payload", return_value=payload),+ patch(+ "products.signals.backend.anomaly_scores.tick.select_insights", side_effect=AssertionError("selected")+ ),+ ):+ plan = plan_tick(None, datetime(2026, 10, 22, tzinfo=UTC))++ assert (plan.enabled, plan.picks) == (False, [])++ def test_invalid_fields_fall_back_and_limits_are_capped(self) -> None:+ config = parse_config(+ {+ "enabled": True,+ "organization_ids": [ORGANIZATION_ID, "not-a-uuid"],+ "allow_team_ids": [2, True, "3"],+ "intervals": ["minute", "week"],+ "max_insights_per_tick": 0,+ "limits": {"max_backfill_buckets": 100_000, "max_series": -1},+ }+ )++ assert config.organization_ids == (ORGANIZATION_ID,)+ assert config.allow_team_ids == (2,)+ assert config.intervals == (IntervalType.WEEK,)+ assert config.max_insights_per_tick == DISABLED.max_insights_per_tick+ assert (config.limits.max_backfill_buckets, config.limits.max_series) == (90, 20)+++class TestTickSelection(SimpleTestCase):+ @parameterized.expand(+ [+ ("never_scored", IntervalType.DAY, UTC, None, datetime(2026, 10, 22, tzinfo=UTC), True),+ # The bucket after 20 October ends at midnight on the 22nd, plus a slot of up to an hour.+ (+ "next_day_ongoing",+ IntervalType.DAY,+ UTC,+ datetime(2026, 10, 20, tzinfo=UTC),+ datetime(2026, 10, 21, 23, tzinfo=UTC),+ False,+ ),+ (+ "next_day_complete",+ IntervalType.DAY,+ UTC,+ datetime(2026, 10, 20, tzinfo=UTC),+ datetime(2026, 10, 22, 1, 1, tzinfo=UTC),+ True,+ ),+ (+ "next_week_ongoing",+ IntervalType.WEEK,+ UTC,+ datetime(2026, 10, 5, tzinfo=UTC),+ datetime(2026, 10, 18, tzinfo=UTC),+ False,+ ),+ (+ "next_week_complete",+ IntervalType.WEEK,+ UTC,+ datetime(2026, 10, 5, tzinfo=UTC),+ datetime(2026, 10, 19, 1, 1, tzinfo=UTC),+ True,+ ),+ ]+ )+ def test_an_insight_is_due_once_the_bucket_after_its_newest_scored_one_completes(+ self,+ _name: str,+ interval: IntervalType,+ timezone: ZoneInfo,+ newest_bucket: datetime | None,+ now: datetime,+ due: bool,+ ) -> None:+ insight = WatchedInsight(team_id=1, insight_id=7, interval=interval)++ assert is_due(insight, newest_bucket, timezone, now) is due+++class TestScoreEvents(SimpleTestCase):+ def test_a_revisit_emits_every_bucket_after_the_newest_scored_one_of_each_series(self) -> None:+ starts = [datetime(2026, 10, day, tzinfo=NEW_YORK) for day in range(15, 20)]+ scores = _scores(_series("scored", starts), _series("new", starts))++ events = score_events(+ team_id=1,+ insight_id=7,+ scores=scores,+ scored_through={"scored": starts[2].astimezone(UTC)},+ scored_at=datetime(2026, 10, 22, tzinfo=UTC),+ )++ emitted = [+ (event["properties"]["$anomaly_series_key"], event["properties"]["$anomaly_bucket_start"])+ for event in events+ ]+ assert emitted == [("scored", start.isoformat()) for start in starts[3:]] + [+ ("new", start.isoformat()) for start in starts+ ]++ 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(+ series_key="series",+ label="Pageviews",+ breakdown_value=None,+ points=[+ ScoredPoint(bucket_start=start, value=3.0, score=None, flag=None, details={}),+ ScoredPoint(+ bucket_start=start + timedelta(days=1), value=90.0, score=0.99, flag=True, details={"mean": 3.0}+ ),+ ],+ )++ def emit_at(scored_at: datetime) -> list[dict[str, Any]]:+ return score_events(team_id=1, insight_id=7, scores=_scores(series), scored_through={}, scored_at=scored_at)++ warm_up, flagged = emit_at(datetime(2026, 10, 22, tzinfo=UTC))++ assert warm_up["event"] == "$anomaly_score"+ assert warm_up["distinct_id"] == SCORER_DISTINCT_ID+ assert warm_up["timestamp"] == "2026-10-19T04:00:00+00:00"+ assert warm_up["options"] == {"process_person_profile": False}+ assert warm_up["properties"]["$anomaly_bucket_start"] == "2026-10-19T00:00:00-04:00"+ assert (warm_up["properties"]["$anomaly_score"], warm_up["properties"]["$anomaly_flag"]) == (None, None)+ assert (flagged["properties"]["$anomaly_score"], flagged["properties"]["$anomaly_flag"]) == (0.99, 1)+ # A resent bucket keeps its uuid, so ClickHouse replaces the earlier row instead of adding one.+ assert [event["event_uuid"] for event in emit_at(datetime(2026, 10, 23, tzinfo=UTC))] == [+ warm_up["event_uuid"],+ flagged["event_uuid"],+ ]+++class TestEligibleTeams(BaseTest):+ @parameterized.expand(+ [+ ("every_team_of_the_organization", [], [], False, {"main", "second"}),+ ("allowlist", ["main"], [], False, {"main"}),+ ("skip_list", [], ["main"], False, {"second"}),+ ("quota_limited", [], [], True, {"main"}),+ ]+ )+ def test_only_allowed_teams_under_their_event_quota_are_scored(+ self, _name: str, allow: list[str], skip: list[str], second_limited: bool, expected: set[str]+ ) -> None:+ teams = {"main": self.team, "second": Team.objects.create(organization=self.organization, name="second")}+ other = Organization.objects.create(name="other")+ Team.objects.create(organization=other, name="other")+ config = AnomalyScoringConfig(+ enabled=True,+ organization_ids=(str(self.organization.id),),+ allow_team_ids=tuple(teams[name].id for name in allow),+ skip_team_ids=tuple(teams[name].id for name in skip),+ )+ limited = [teams["second"].api_token] if second_limited else []++ with patch("products.signals.backend.anomaly_scores.queue.list_limited_team_attributes", return_value=limited):+ eligible = {team.id for team in eligible_teams(config)}++ assert eligible == {teams[name].id for name in expected}+++class TestWatchedInsights(BaseTest):+ @time_machine.travel(NOW, tick=False)+ def test_only_watched_time_series_trends_with_a_scored_interval_are_eligible(self) -> None:+ dashboard = Dashboard.objects.create(team=self.team, name="Main")+ insights = {+ name: Insight.objects.create(team=self.team, name=name, query=query)+ for name, query in {+ "on_dashboard": _trends(interval="day"),+ "viewed": _trends(interval="week"),+ "default_interval": _trends(),+ "hourly": _trends(interval="hour"),+ "funnel": {"kind": "InsightVizNode", "source": {"kind": "FunnelsQuery", "series": []}},+ "bold_number": _trends(interval="day", trendsFilter={"display": "BoldNumber"}),+ "cumulative": _trends(interval="day", trendsFilter={"display": "ActionsLineGraphCumulative"}),+ "deleted_tile": _trends(interval="day"),+ "unwatched": _trends(interval="day"),+ }.items()+ }+ for name in ("on_dashboard", "default_interval", "hourly", "funnel", "bold_number", "cumulative"):+ DashboardTile.objects.create(dashboard=dashboard, insight=insights[name])+ DashboardTile.objects.create(dashboard=dashboard, insight=insights["deleted_tile"], deleted=True)+ record_insight_view(insight_id=insights["viewed"].id, team_id=self.team.id, user_id=self.user.id)++ watched = watched_insights([self.team.id], (IntervalType.DAY, IntervalType.WEEK, IntervalType.MONTH), NOW)++ names = {insight.id: name for name, insight in insights.items()}+ assert {(names[insight.insight_id], insight.interval) for insight in watched} == {+ ("on_dashboard", IntervalType.DAY),+ ("viewed", IntervalType.WEEK),+ ("default_interval", IntervalType.DAY),+ }+++class TestScoredBuckets(ClickhouseTestMixin, BaseTest):+ def test_reads_the_newest_scored_bucket_of_each_series(self) -> None:+ def score(insight_id: int, series_key: str, bucket: datetime, source: str = "insight") -> None:+ _create_event(+ team=self.team,+ event="$anomaly_score",+ distinct_id=SCORER_DISTINCT_ID,+ timestamp=bucket,+ properties={+ "$anomaly_insight_id": insight_id,+ "$anomaly_series_key": series_key,+ "$anomaly_source": source,+ },+ )++ score(7, "a", datetime(2026, 10, 18, tzinfo=UTC))+ score(7, "a", datetime(2026, 10, 20, tzinfo=UTC))+ score(7, "b", datetime(2026, 10, 19, tzinfo=UTC))+ score(8, "c", datetime(2026, 10, 21, tzinfo=UTC))+ score(9, "d", datetime(2026, 10, 21, tzinfo=UTC), source="default")+ score(10, "e", datetime(2026, 1, 1, tzinfo=UTC))+ # Any member can capture these: one names an insight of another team, one a bucket in the future.+ score(11, "f", datetime(2026, 10, 21, tzinfo=UTC))+ score(8, "c", datetime(2027, 1, 1, tzinfo=UTC))++ state = scored_buckets(+ self.team,+ insight_ids={7, 8, 9, 10},+ since=datetime(2026, 9, 1, tzinfo=UTC),+ now=datetime(2026, 10, 22, tzinfo=UTC),+ )++ assert state == {+ 7: {"a": datetime(2026, 10, 20, tzinfo=UTC), "b": datetime(2026, 10, 19, tzinfo=UTC)},+ 8: {"c": datetime(2026, 10, 21, tzinfo=UTC)},+ }++ @time_machine.travel(NOW, tick=False)+ def test_select_insights_picks_due_insights_never_scored_first(self) -> None:+ dashboard = Dashboard.objects.create(team=self.team, name="Main")+ insights = {+ name: Insight.objects.create(team=self.team, name=name, query=_trends(interval="day"))+ for name in ("never_scored", "stale", "fresh")+ }+ for insight in insights.values():+ DashboardTile.objects.create(dashboard=dashboard, insight=insight)+ cache.delete(_cooldown_key(insight.id))+ today = NOW.replace(hour=0)+ for name, bucket in (("stale", today - timedelta(days=3)), ("fresh", today - timedelta(days=1))):+ _create_event(+ team=self.team,+ event="$anomaly_score",+ distinct_id=SCORER_DISTINCT_ID,+ timestamp=bucket,+ properties={+ "$anomaly_insight_id": insights[name].id,+ "$anomaly_series_key": name,+ "$anomaly_source": "insight",+ },+ )+ config = AnomalyScoringConfig(+ enabled=True, organization_ids=(str(self.organization.id),), allow_team_ids=(self.team.id,)+ )++ with patch("products.signals.backend.anomaly_scores.queue.list_limited_team_attributes", return_value=[]):+ selection = select_insights(config, SelectionConfig(weighting=Weighting.UNIFORM), NOW, limit=10)++ assert [pick.insight_id for pick in selection.picks] == [insights["never_scored"].id, insights["stale"].id]+ assert selection.picks[1].scored_through == {"stale": today - timedelta(days=3)}diff --git a/tach.toml b/tach.tomlindex 51259725e37b..fb1d84be2d16 100644--- a/tach.toml+++ b/tach.toml@@ -904,6 +904,8 @@ depends_on = ["posthog","products.actions","products.alerts",+ # Facade-only: anomaly scoring spreads insights over the window after each boundary like alert checks.+ "products.alerts_platform","products.cdp","products.conversations","products.dashboards",